Вопрос

I would like to return the page Entity from the method: getByPath($path). I just would like to know where this method should be in the script. Inside the controller or inside the entity class?

In my opinion the entity "Page" shouldn't have a function called "getByPath()" since an entity should only contain database information of one entity, which can be get or set by getters and setters. And this "getByPath" function is not just a getter or setter it requires me to run the entitymanager within the entity. Am I right?

So am I right that I should make a PageController and create the "getByPath()" (which will return the page object) function there? Or would anyone create that function inside the entity class?

I would like to know what the nicest way is to accomplish this.

Thanks in advance.

Это было полезно?

Решение

You should put that function inside a custom repository for the Page entity

While the Entities are the objects you are storing, the Repository is the class that provides methods to access/load those objects, eg when you call $em->getRepository('Entities\Page')->find($page_id);, you call the find() method on your Page repository and it's its job to find it for you.

Doctrine provides a default repository for each entity (with the various find*() methods, ...), but you can provide a custom one where you can add your own method, such as getByPath().

Symfony 2 - Database and Doctrine - Custom Repository Classes

Doctrine 2 - Custom repository

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top