I have an entity (layout-template) that will have up to 10 configurable sidebars that are all instances of one and the same entity type.
My solution right now is to put it out like this (below)
/**
* @var object BizTV\ContainerManagementBundle\Entity\Container
*
* @ORM\ManyToOne(targetEntity="BizTV\ContainerManagementBundle\Entity\Container")
* @ORM\JoinColumn(name="sidebar1", referencedColumnName="id", nullable=true)
*/
protected $sidebar1;
/**
* @var object BizTV\ContainerManagementBundle\Entity\Container
*
* @ORM\ManyToOne(targetEntity="BizTV\ContainerManagementBundle\Entity\Container")
* @ORM\JoinColumn(name="sidebar2", referencedColumnName="id", nullable=true)
*/
protected $sidebar2;
but I realize the best way would be to store them in an array of objects (which also wouldn’t restrict me to 10 sidebaras in the future). If someone could point me in the right direction here. The hardest thing to do is the form where I pick these 10 entities. My way as above would be a simple drop-down for each of the ten properties.
->add('sidebar1', 'entity', array(
'label' => 'Choose sidebar ',
'empty_value' => 'Not active',
'class' => 'BizTVContainerManagementBundle:Container',
'property' => 'select_label',
'query_builder' => function(EntityRepository $er) use ($company) {
return $er->createQueryBuilder('u')
->where('u.company = :company')
->setParameters( array('company' => $company) )
->orderBy('u.name', 'ASC');
},
))
->add('sidebar2', 'entity', array(
'label' => 'Choose sidebar ',
'empty_value' => 'Not active',
'class' => 'BizTVContainerManagementBundle:Container',
'property' => 'select_label',
'query_builder' => function(EntityRepository $er) use ($company) {
return $er->createQueryBuilder('u')
->where('u.company = :company')
->setParameters( array('company' => $company) )
->orderBy('u.name', 'ASC');
},
))
Again, I realize this is far from the best way of doing it, but being new to Symfony2 I don’t really know how to go about it.
Use a
Many to Manyrelationship and for the forms, use the “collection” field type:http://symfony.com/doc/master/cookbook/form/form_collections.html