I have this code
->add('user', 'entity', array(
'class' => 'Acme\Entity\User',
'query_builder' => function(EntityRepository $er) use ($options)
{ return $er->createQueryBuilder('u')
->orderBy('u.name', 'ASC');
},
'data' => $option['id']
))
Its not working
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('description')
->add('user', 'entity', array(
'class' => 'Acme\Entity\User',
'query_builder' => function(EntityRepository $er) use ($options) {
return $er->createQueryBuilder('u');
},
'preferred_choices' => array('2')
))
;
}
The form should map the user->id value automatically to the selected entity select field. For example if your have a Computer entity that has a OnetoOne relationship with a User entity in a join table called 'computer_users':
The field 'user' in the form class should pick up the user->id value from the 'user' attribute object in the Computer class passed to the form.
Alternatively, you can explicitly set the value by explicitly passing the user entity into the form using SetData():
You can use the one of the following:
Set a default value in the object
Use a preferred choices option in the form builder:
Or set
'property_path' => false
and use a'data' => YourDefaultEnity