symfony - Symfony2 embedded forms with the "child" entity assigned in a dynamic way -
i have project entity tied tolerance entity.
on surface project has 1 tolerance seems one-to-one, in practice want keep track of changes in tolerance entity. so, every time project saved, if tolerance entity fields changed want save new "version" of tolerance, recording timestamp , did change.
in visualization, every time user sees project, or edit it, see last version of tolerance attached.
the problem have no clue how manage thing point of view of form.
as tolerance entity can referred other stuff other project, have projecttolerance extending tolerance.
this project entity:
/** * @orm\entity(repositoryclass="appbundle\entity\projectrepository") * @orm\table(name="projects") */ class project { /** * @orm\column(type="integer") * @orm\id * @orm\generatedvalue(strategy="auto") */ protected $id; /** * @orm\column(type="string", length=100) */ protected $name; //other fields
this base tolerance:
/** @orm\mappedsuperclass */ class tolerance { /** * @orm\column(type="integer") * @orm\id * @orm\generatedvalue(strategy="auto") */ protected $id; /** * @orm\column(type="integer") */ protected $risk; /** * @orm\column(type="integer") */ protected $scope; // other fields /** * @orm\column(type="datetime") */ protected $timestamp; /** * @orm\manytoone(targetentity="user") */ protected $changedby; public function __construct() { $this->timestamp = new \datetime(); }
and extension:
/** * @orm\entity(repositoryclass="appbundle\entity\projecttolerancerepository") * @orm\table(name="project_tolerances") */ class projecttolerance extends basetolerance { /** * @orm\manytoone(targetentity="project") */ protected $project; /** * set project * * @param \appbundle\entity\project $project * @return projecttolerance */ public function setproject(\appbundle\entity\project $project = null) { $this->project = $project; return $this; }
do need specify relationship in project entity onetomany relationship well? imagine if need tweak "gettolerance" gets last version of tolerance...
how manage forms?
edit: here creation of form (inside controller, i'll take out independent form type):
$form = $this->createformbuilder($project) ->add('name', 'text') ->add('approach', 'text') ->add('background', 'text') ->add('scope', 'text') ->add('scopeexclusions', 'text') ->add('interfaces', 'text') ->add('save', 'submit', array('label' => 'create project')) ->getform(); $form->handlerequest($request);
what i'm missing like:
$builder->add('tolerances', 'collection', array('type' => new projecttoleranceformtype(), 'allow_add' => true, 'label' => false, 'by_reference' => false, ))
thank you!
it's one-to-many relation. have add next models classes:
/** @orm\mappedsuperclass */ class project { ... /** * @orm\onetomany( * targetentity="tolerance", * mappedby="project", * orphanremoval=true * ) **/ private $tolerances; public function __construct() { $this->tolerances= new \doctrine\common\collections\arraycollection(); } ... public function gettolerances() { return $this->tolerances; } public function addtolerance(tolerance $tolerance) { $this->tolerances->add($tolerance); $tolerance->setproject($this); } public function removetolerance(tolerance $tolerances) { $this->tolerances->removeelement($tolerances); $tolerances->setproject(null); } } /** @orm\mappedsuperclass */ class tolerance { ... /** * @orm\manytoone(targetentity="project", inversedby="tolerances") * @orm\joincolumn(nullable=false) */ private $project; ... public function getproject() { return $this->project; } public function setproject(project $project = null) { $this->project= $project; } }
next step when submit project form set tolerance data.
read more doctrine relations.
for createdat , updatedat fields can use timestampable doctrine extension.
Comments
Post a Comment