I run this code to get my select control and it works fine.
$builder
->add('access', 'entity', array(
'label' => 'Behörigheter',
'multiple' => true, // Multiple selection allowed
'expanded' => true, // Render as checkboxes
'property' => 'name',
'class' => 'BizTV\ContainerManagementBundle\Entity\Container',
'query_builder' => function(\Doctrine\ORM\EntityRepository $er) use ($company) {
$qb = $er->createQueryBuilder('a');
$qb->innerJoin('a.containerType', 'ct');
$qb->where('a.containerType IN (:containers)', 'a.company = :company');
$qb->setParameters( array('containers' => array(1,2,3,4), 'company' => $company) );
$qb->orderBy('ct.id', 'ASC');
return $qb;
}
));
Now I want to be able to customize he label for the checkboxes, I learnt just the other day that by changing property to select_label and defining that function in the entity you can accomplish this, but with this query it doesn’t work.
Is this because I am joining? Is there a way to accomplish it?
This is the code that doesn’t work
$builder
->add('access', 'entity', array(
'label' => 'Behörigheter',
'multiple' => true, // Multiple selection allowed
'expanded' => true, // Render as checkboxes
'property' => 'select_label',
'class' => 'BizTV\ContainerManagementBundle\Entity\Container',
'query_builder' => function(\Doctrine\ORM\EntityRepository $er) use ($company) {
$qb = $er->createQueryBuilder('a');
$qb->innerJoin('a.containerType', 'ct');
$qb->where('a.containerType IN (:containers)', 'a.company = :company');
$qb->setParameters( array('containers' => array(1,2,3,4), 'company' => $company) );
$qb->orderBy('ct.id', 'ASC');
return $qb;
}
));
In the entity I have:
public function getSelectLabel()
{
return $this->name . ' (' . $this->parent->getName() . ')';
}
It works fine for another form that has a SELECT (single choice) control, but not for this one…
The error I get is
Fatal error: Call to a member function getName() on a non-object in /var/www/biztv_symfony/src/BizTV/ContainerManagementBundle/Entity/Container.php on line 190
Oh sorry guys, I had a few objects in there that didn’t have $this->parent defined, I rewrote my name function as follows and it works fine.
Thanks all!