Monday 15 July 2013

Form post-processing in Symfony2 -



Form post-processing in Symfony2 -

i new of symfony, , trying create form bound entity user.

one field of entity of type arraycollection. onetomany relationship objects of class. so, little bit of code clearer.

class user { \\... /** * @orm\onetomany(targetentity="usergoods", mappedby="users") * @orm\joincolumn(name="goods", referencedcolumnname="id") */ private $goods; public function __construct() { $this->goods = new arraycollection(); } \\... }

and associated class

class usergoods { /** * @var integer * * @orm\column(name="id", type="integer") * @orm\id * @orm\generatedvalue(strategy="auto") */ private $id; /** * @var \datetime * * @orm\column(name="inserted_at", type="datetime") */ private $insertedat; /** * @var float * * @orm\column(name="value", type="float") */ private $value; /** * @orm\manytoone(targetentity="user", inversedby="goods") */ protected $users; }

now, want create formbuilder extremely simple, yet couldn't figure out how myself. want field of type number, , if object of type goods current date exists, modify it, otherwise add together new object collection.

this done within controller, have a lot of instances of form, , create programme impossible maintain.

is there way add together post-processing of submitted info within form builder? tried datatransformers these won't suffice, @ transform number usergoods object, , original arraycollection not preserved (and doctrine associations?). in addition, if declare field type collection of number types, all items within arraycollection displayed when rendering form, not lastly one.

any thought on how out of this? give thanks in advance help.

as suggested, utilize form events. within event check if goods submitted date exist (load them database) , modify them post data. if dont exist, creating new ones. can create method in entity, getlastitemsincollection(), can utilize criteria, load lastly 1 database (recommended), or lastly item original arraycollection. can create field unmapped, , map goods manually in formevent, described above. hope helps , hope understood correctly.

forms symfony2 doctrine2 formbuilder

No comments:

Post a Comment