I am currently developing a symfony project for which I need a form to edit an existing entity called Location.
One Location object can hold 0 to n Machines. The form should contain multiple checkboxes, with preselected values depending on weather the entity Location already contains any Machine or not. If yes, preselect the checkbox, if not leave it empty.
The Location:
...
class Location
{
/**
* @ORM\OneToMany(targetEntity="Machine", mappedBy="location")
*
*/
private $machines;
...
The Machine:
...
class Machine
{
/**
* @ORM\ManyToOne(targetEntity="Location", inversedBy="machines")
* @ORM\JoinColumn(name="location_id", referencedColumnName="id")
*/
private $location;
...
I build the form like this:
$form = $this->createFormBuilder($location)
->add('name', null , array( 'label' => 'Roomname: ', 'attr'=> array( 'placeholder'=>'Enter a name') ) )
->add('descr', null , array( 'label' => 'Description: ', 'attr'=> array( 'placeholder'=>'Enter a description') ) )
->add('machines', 'entity', array('class' => 'PRwissHostsBundle:Machine',
'property' => 'name',
'required' => false,
'expanded' => true,
'multiple' => true,
))
->getForm();
The code above results in a form with a couple of checkboxes with the entities name next to it. I want to pre select those checkboxes where the machines already are related to the location.
I have had a look at this question, but unfortunatly the "data" => x only refers to one value.
Any ideas how I can accomplish this? Thanks in advance!