0

My question is about creating collection of entities. I know about "How To Embed Collection Forms" and successfully used it. But in this case I have:

Simple class

class Thing
{
  /**              
  * @ORM\ManyToMany(targetEntity="DicStyle", mappedBy="things")
  * ....
  */
  protected $styles;   

  public function __construct()
  {
     $this->styles = new ArrayCollection();
  } 
}

Dictionary of styles

class DicStyle 
{
    ..... 
}

I don't need to create form for DicStyle objects, because this is read only objects = dictionary (unchangeable). So, I want to create a form with something like this:

$builder->add('styles', 'collection', array(
    'type'   => 'entity', 'options' => array(
        'class' => 'MyEntityBundle:DicStyle'
        )
 ))         

Of course it's pseudo-code. I can not imagine how to implement it.

The result

Suppose, I have:

  • Table "Thing" with one row (id = 1).
  • Table "DicStyle" with 6 rows (id = from 1 to 6).
  • Table "mtm_thing_dicstyle" (many-to-many table)

In the form, I choose two DicStyle (id=3, id=5) for the Thing. So, the mtm_thing_dicstyle contains:

thing_id     dicstyle_id
--------     ------------
    1            3
    1            5
user3534709
  • 33
  • 1
  • 6
  • I'm not really sure what you're trying to accomplish. Create a form but not persist data witht hat form? – Koalabaerchen May 22 '15 at 11:54
  • @Koalabaerchen . I'm trying to create 2 fields in the form and save this 2 fields in one array ("styles"). After saving the form, field "styles" have to contain [dic_style_3, dic_style_5] – user3534709 May 22 '15 at 20:01
  • **Thanks for all.** Solution is here: [Symfony2 collection of Entities - how to add/remove association with existing entities?][1] [1]: http://stackoverflow.com/questions/11089861/symfony2-collection-of-entities-how-to-add-remove-association-with-existing-en?rq=1 – user3534709 May 23 '15 at 06:04

2 Answers2

0

Try this form:

 $builder->add('styles', 'entity', array(
     'class' => 'MyEntityBundle:DicStyle'
     'property'=>'name' //what property do you want to see when you select,
     'multiple" => true //you'll be able to select many DicStyle
     'expanded' => false //it'll shown on a multiple choice select tag    
    )
);
Fabrice Kabongo
  • 671
  • 10
  • 23
  • In this case, field "styles" will contain only 1 entity "DicStyle". But I have many-to-many relations, and that means: `public function __construct()` `{` `$this->styles = new ArrayCollection();` `}` As I wrote, field "styles" has 2 enities "DicStyle". So, I need 2 field "DicStyle" in the form. Because of this I've tried to use "collection". – user3534709 May 22 '15 at 18:59
0

To select one or more objects in a form to relate them to your result you can use the form field type "entity". (See form-types)

$builder->add('styles', 'entity', array(
    'class' => 'MyEntityBundle:DicStyle',
    'property' => 'name', // property you want to be displayed
    'expanded' => true, 
    'multiple' => true
    )
);

This would render checkboxes and therefore the possibility to select multiple entities to be referenced.

Keep in mind, if you use multiple => true there are two options:

  • expanded => false: renders a multiselect field
  • expanded => true: renders checkboxes

See form-entity-type-options

DerStoffel
  • 2,553
  • 2
  • 15
  • 25
  • In this case, field "styles" will contain only 1 entity "DicStyle". But I have many-to-many relations, and that means: `public function __construct()` `{` `$this->styles = new ArrayCollection();` `}` As I wrote, field "styles" has 2 enities "DicStyle". So, I need 2 field "DicStyle" in the form. Because of this I've tried to use "collection". – user3534709 May 22 '15 at 18:54
  • This is not right, the parameter 'multiple' => true allows to select more than one "DicStyle", not just one. There is no need at all, to use a collection. – DerStoffel May 26 '15 at 07:09