Define a custom method in Doctrine\\ORM\\Persisten

2019-06-22 05:37发布

问题:

I'm fetching data with a many-to-many relationship and I want to define acustom method. Is it possible (and how) to use something like:

$hotel = $entityManager->getRepository('Hotels')->findOneById(1);
$types = $hotel->getTypes(); //$types is instance of **Doctrine\ORM\PersistentCollection**
$types->myCustomFunction(); //do something

回答1:

It seems to be not implemented already.

See this Doctrine's JIRA issue : http://www.doctrine-project.org/jira/browse/DDC-547

Consider allowing custom PersistentCollection implementations

We should consider allowing the configuration of custom PersistentCollection implementations on a per-association basis. This could allow users to craft optimized (SQL) behavior for for some of their collections to improve performance without changing the domain model code.

For this, PersistentCollection needs to be designed for inheritance.