symfony - List entities belonging to an association in Doctrine -


let's have "person" entity. person can belong "group". associated through manytomany, join table strategy.

the general code looks this:

/**  * vendor\acmebundle\entity\person  *  * @orm\entity(repositoryclass="vendor\acmebundle\entity\personrepository")  */ class person extends baseuser {  /**  * @orm\column(type="integer")  * @orm\id  * @orm\generatedvalue(strategy="auto")  */ protected $id; /**  * @orm\manytomany(targetentity="vendor\acmebundle\entity\group")  */ protected $groups;  } 

and group entity

/**  * @orm\entity  */ class group extends basegroup {     /**      * @orm\id      * @orm\column(type="integer")      * @orm\generatedvalue(strategy="auto")      */     protected $id;     /**      * @orm\column(type="string", nullable=true)      */     protected $publicname;     } 

what want achieve?

given group, list users belonging group in consistent way, including pagination options (aka limit , offset)

something this:

function getuserfromgroup(group $group, $criteria, $limit, $offset){}; 

considerations:

  • the entities mutable, can adapted achieve requisite (e.g. association changed unidirectional bidirectional)
  • the amount of person entities in thousands (2000~8000)
  • the amount of groups less 10

this explained in symfony2 book, chapter on doctrine

for case, suggest using findby() method.

from official doctrine documentation:

function getuserfromgroup($group, $criteria, $limit, $offset){     // should build criteria paramaters array,     // i'll asume it's "fieldname" => "valuetofilterby"     $criteria['groups'] = $group;      $users = $em->getrepository('appbundle\entity\user')         ->findby(             $criteria, // filter columns             array('name' => 'asc'), // sorting             $limit, // how many entries select             $offset   // offset         );      return $users; }; 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -