symfony - how to get the most used tags with Doctrine? -
i have 2 entities, "pin" , "tag", manytomany relation. want list of tags order used display in form
here repository method :
public function findpopulartag(){ return $qb = $this->createquerybuilder('t') ->addselect('count(distinct p.id) total_pins') ->leftjoin('t.listeepingles', 'p') ->groupby('t.id') ->orderby('total_pins','desc'); }
in buildform use method create entitytype :
$builder->add( 'listetagsdanslisteepingles', 'entity', array( 'class' => 'sharincookrecipesbundle:tag', 'required' =>false, 'multiple' => true, 'property' => 'libelle', 'expanded'=>true, 'query_builder' => function(tagrepository $repo){ return $repo->findpopulartag(); }, ));
but have error : expected argument of type "object or array", "string" given in vendor/symfony/symfony/src/symfony/component/propertyaccess/propertyaccessor.php @ line 224
you can use left join , order in query builder
$tag = $em->getrepository('namespaceyourbundle:tag'); $qb = $tag->createquerybuilder('t') ->addselect('count(distinct p.id) hidden total_pins') ->leftjoin('t.pins', 'p'); ->groupby('t.id') ->orderby('total_pins','desc') ->getquery() ->getresult();
Comments
Post a Comment