Ali
Ali

Reputation: 493

what's the difference of Entities in symfony 2 and models in other frameworks?

Well I'm new in symfony framework. MVC contains module but as I realized, in symfony Entities are like models .
I always write my database related functions and queries in modules. now, when I want to use $this->getdoctrine() in my entity it Certainly doesn't work .

So what's the difference of Entities in symfony2 and models in other frameworks ?
Is it okay to write the database queries and functions in controller?

Upvotes: 0

Views: 242

Answers (1)

DonCallisto
DonCallisto

Reputation: 29922

Actually, Model in Symfony is a bit fuzzier than other MVC frameworks.

Entities should be used only to keep data that describes DB tables so, no logic within them (except for callbacks method of validation, if you want).

Controllers are "standard" controllers of MVC model/paradigm/pattern so you need to keep them "smaller" and more "slenders" as possible.

So, what's the solution?

In my opinion, you can define services (or classes, or handlers, or whatever you want to call them) where you'll define the heavy business logic.

Why? Controllers aren't made for that kind of tasks and are not reusable (DRY). They are made only for accept a request and produce a response.

As, in symfony2, nearly everything that keeps logic is a service (and, btw, if you need to pass parameters to your "business-logic-class" the best way is to inject them), I strongly recommend to study them and to migrate your logic there.

Just to make my answer more in topic with your question, the login check action you're trying to write here (that, btw, you can accomplish with some bundles like FOSUserBundle) could be raised as a kind of event that an EventListener or EventSubscriber listen to and let them to implement all logic.

Upvotes: 1

Related Questions