formssymfonydoctrine-mongodb

How to create a form field which is referenced to another document using formbuilder in symfony2?


In my document I have a category member attribute which is referenced to the same class Category

/**
 * Parent Category's path ( Indexed )
 *
 * @var String
 * @MongoDB\ReferenceOne( targetDocument = "Category", simple="true" )
 */
protected $parent;

Now inside my form I want to make parent a select dropdown field and show all my categories in that

$builder->add( 'parent', 'choice', array(                                                                                        
    'choices' => array( '..', '..' )
));                                                                                                               

How do I show all my categories in dropdown and map that dropdown field so when form is submitted the parent field contains object ID of parent field


Solution

  • Use document field type:

    $builder->add('parent', 'document', array(
        'class' => 'AcmeDemoBundle:Category',
        'property' => 'name_of_property_to_display_as_item_label',
    ));