I’m trying to create a simple form with sonata bundle. I have one problem trying to load data entity type field. I know this is working:
$formMapper->add( 'foo', 'entity', array(
'class' => 'myVendorMyBundleBundle:Foo',
'property' => 'id',
'query_builder' => function(FooRepository $er) {
return $er->createQueryBuilder('qb')
->add( 'select', 'f' )
->add( 'from', 'myVendorMyBundleBundle:Foo f' )
},
'label' => 'foo'
) );
The problem is I don’t want to show entity id, I want to show its name, which is in a related table. I tried to use join statement at createQueryBuilder method, but I didn’t get it to work:
$formMapper->add( 'foo', 'entity', array(
'class' => 'myVendorMyBundleBundle:Foo',
'property' => 'b.name',
'query_builder' => function(FooRepository $er) {
return $er->createQueryBuilder('qb')
->add( 'select', 'f' )
->add( 'from', 'myVendorMyBundleBundle:Foo f' )
->add( 'join', 'myVendorMyBundleBundle:Bar b' )
},
'label' => 'foo'
) );
How can I do this?
You should make a
__toString()method on the entity you are using, Symfony will automatically use that as the label.Edit:
In your
myVendorMyBundleBundle:Fooclass you should have a variable$bardefinedDoctrine will lazy load in the association (not an issue if you are working with a low/singular number of labels)…I have used this method effectively to do what you are describing