1

Reading Symfony documentation I did not see that talking about Models.

The first thought I thought: I do not want to mix business logic

I dont want DQL in my controller actions. What about MVC than.

My idea is next:

  • Create new directory in bundle with name Models

  • Set __ namespace __ for that model and ( use ) attach the necessary Doctrine class

  • In my Model class i put DQL logic connected with Entity

Next in controller use current Model.

Just simple controller action whitout mixing DQL in controller

  use Company\Bundle\Models\MyModel;

 public function getRecentMembersAction($max = 3)
    {
        $model = new Model() // get model
        $list = $model->getRecentMembers($max); // DQL

        // Render
        return $this->render('CompanyBundle:Controller.index.html.twig', array('list'=>$list);
    }

My question is whether this is a good idea and good practice?

SLOVED: http://docs.doctrine-project.org/en/latest/reference/annotations-reference.html#entity

1 Answer 1

5

Keep your DQL in repositories. And keep your domain logic in the Service Layer.

Controller Layer → Service Layer → Repository Layer

This way you can have several controller types — HTML, REST, command line — all using the same domain logic encapsulated in the Service Layer.

And the Repository Layer encapsulates all the persisting functionality so that you can swap an ORM Repository Layer with an ODM Repository Layer or some other type of Repository Layer — like Web service calls, persisting to text files, etc.

Sign up to request clarification or add additional context in comments.

3 Comments

I was just about to answer the same :) +1
Just a second. my entity Product must have one more class inside Entity directory ProductRepository right. And with annotation in Product Entity i set what repositoryClass need to use. And my ProductRespository contain DQL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.