php - Symfony 2 forms: array of entities -
i trying build form multiple entities. let me first introduce sample classes:
for clarity not show annotations or abbreviate them, , not show use or namespace commands.
/** * entity class * @orm ... mapping orm */ class entitya { /** * @var modelarray * @orm\column(name="...", type="object") */ private $modelarray; // getters , setters, default constructor } /** * array wrapper, keeping array unique, sorting criteria etc. * @orm(...) */ class modelarray { /** * @var array<a> */ private $array; // getter, adder, remover, constructor, other private logic }
notice class modelarray stores objects of given type a:
/** * 1 more model */ class { /** * @var boolean */ private $bool; /** * @var string */ private $string; // getters, setters }
i chose data structure, because never need objects of exist outside entitya class, , keep logic out of entity, chose implement modelarray class in between. entitya class persisted database, , child objects.
now want create form, can edit $->bool attributes of instances of array of entitya @ once. provide array<entitya>
.
i proceed follows:
//in controller action $data = array( 'as' => $arrayofentitya, ); $form = $this->createform(new gridtype(), $data); // create view, render
my form type this:
class gridtype extends abstracttype { public function getname() { return 'a_grid'; } public function buildform(formbuilderinterface $builder, array $options) { $builder->add('as', 'collection', array( 'type' => new gridatype(), )); } }
and use form type
class gridatype extends abstracttype { public function getname() { return 'a_grid_a'; } public function buildform(formbuilderinterface $builder, array $options) { $builder->add('modelarray', new modelarraytype()); } public function getdefaultoptions(array $options) { return array( 'data_class' => 'entitya', ); } }
with model array type
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' => 'modelarray', ); } }
notice // ???
: edit boolean attribute of strored in modelarray, , think appropriate form type continue 'collection'. can't find out how use array ($modelarray->array
)here collection. how should this?
an then, i'm not entirely sure if practice implement such lot of form types achieve 1 usable form. there different , better way?
Comments
Post a Comment