Note that there are some explanatory texts on larger screens.

plurals
  1. POSymfony 2 forms: array of entities
    primarykey
    data
    text
    <p>I am trying to build a form for multiple entities. Let me first introduce some sample classes:</p> <p>For clarity I do not show all annotations or abbreviate them, and I do not show the use or namespace commands.</p> <pre><code>/** * The Entity class * @ORM ... mapping to ORM */ class EntityA { /** * @var ModelArray * @ORM\Column(name="...", type="object") */ private $modelArray; // Getters and Setters, default constructor } /** * An array Wrapper, keeping the array always unique, sorting it by criteria etc. * @ORM(...) */ class ModelArray { /** * @var array&lt;A&gt; */ private $array; // Getter, Adder, Remover, Constructor, other private logic } </code></pre> <p>Notice especially that the class ModelArray stores only objects of a given Type A:</p> <pre><code>/** * One more Model */ class A { /** * @var boolean */ private $bool; /** * @var string */ private $string; // Getters, Setters } </code></pre> <p>I chose this data structure, because I never need objects of A to exist outside the EntityA class, and to keep the logic out of my Entity, I chose to implement the ModelArray class in between. The EntityA class is persisted to a database, and with it the child objects.</p> <p>Now I want to create a form, where I can edit the $->bool attributes of all A instances of an Array of EntityA at once. So what I provide is <code>array&lt;EntityA&gt;</code>.</p> <p>I would then proceed as follows:</p> <pre><code>//In a controller action $data = array( 'as' =&gt; $arrayOfEntityA, ); $form = $this-&gt;createForm(new GridType(), $data); // Create view, render </code></pre> <p>My form type would look like this:</p> <pre><code>class GridType extends AbstractType { public function getName() { return 'a_grid'; } public function buildForm(FormBuilderInterface $builder, array $options) { $builder-&gt;add('as', 'collection', array( 'type' =&gt; new GridAType(), )); } } </code></pre> <p>And use this form type</p> <pre><code>class GridAType extends AbstractType { public function getName() { return 'a_grid_a'; } public function buildForm(FormBuilderInterface $builder, array $options) { $builder-&gt;add('modelArray', new ModelArrayType()); } public function getDefaultOptions(array $options) { return array( 'data_class' =&gt; 'EntityA', ); } } </code></pre> <p>With the model array type</p> <pre><code>class ModelArrayType extends AbstractType { public function getName() { return 'model_array'; } public function buildForm(FormBuilderInterface $builder, array $options) { // ??? } public function getDefaultOptions(array $options) { return array( 'data_class' =&gt; 'ModelArray', ); } } </code></pre> <p>Notice the <code>// ???</code>: I would like to only edit the boolean attribute of the As strored in the ModelArray, and I think the appropriate form type to continue would be a 'collection'. But I can't really find out how to use the array (<code>$modelArray-&gt;array</code>)here as a collection. How should I do this?</p> <p>An then, I'm not entirely sure if it is good practice to implement such a lot of form types just to achieve one usable form. Is there a different and better way?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload