Reputation: 2166
I have a formtype with this entry:
->add('work', 'entity', array(
'class' => 'AcmeAdminBundle:Work',
'property' => 'name',
'query_builder' => function (EntityRepository $repository) {
return $repository->createQueryBuilder('work')
->orderBy('work.name', 'ASC');
},
'multiple' => true,
'required' => false,
'group_by' => 'artists'
))
I'm trying to group the works by their artists name. The problem is that the artists are a ManyToMany like this:
class Work
{
/**
* @ORM\ManyToMany(targetEntity="Artist", inversedBy="works")
* @ORM\JoinTable(name="works_artists")
*/
protected $artists;
I tried setting the group_by to "artists" and "artists.name" but neither work. I only get a PHP error.
Does anyone know how to do it or if it is possible?
Upvotes: 1
Views: 1546
Reputation: 64476
Use 'group_by' => 'artists.name'
in query builder
->add('work', 'entity', array(
'class' => 'AcmeAdminBundle:Work',
'property' => 'name',
'query_builder' => function (EntityRepository $repository) {
return $repository->createQueryBuilder('work')
->orderBy('work.name', 'ASC');
},
'multiple' => true,
'required' => false,
'group_by' => 'artists.name'
))
Edit try using choices
option in the entity type field i have called another function which creates the choice list as group of arrays like group of for each artist with their workers make sure you properly define the getters in the function i have used dummy like getName()
->add('work', 'entity', array(
'class' => 'AcmeAdminBundle:Work',
'multiple' => true,
'required' => false,
'choices' => $this->getOptGropupForEntities()
))
public function getOptGropupForEntities(){
$artists = $this->em->getRepository('AcmeAdminBundle:Artists')->findAll();
$list = array();
foreach($artists as $a){
$name = $a->getName();
if(count($a->getWorks())>0){
foreach($a->getWorks() as $w){
$list[$name][$w->getId()] = $w->getName();
}
}
}
return $list;
}
Upvotes: 5