Tuesday, 15 April 2014

symfony2 - Why Doctrine does not save entity when multiple set to false? -



symfony2 - Why Doctrine does not save entity when multiple set to false? -

i have entity:

<?php namespace bw\blogbundle\entity; utilize doctrine\common\collections\arraycollection; utilize doctrine\orm\mapping orm; /** * postcustomfield * * @orm\table(name="post_custom_field") * @orm\entity(repositoryclass="bw\blogbundle\entity\postcustomfieldrepository") */ class postcustomfield { /** * @var arraycollection * * @orm\manytomany(targetentity="bw\blogbundle\entity\customfieldproperty", inversedby="postcustomfields") */ private $customfieldproperties; public function __construct() { $this->customfieldproperties = new arraycollection(); } /** * add together customfieldproperties * * @param \bw\blogbundle\entity\customfieldproperty $customfieldproperties * @return postcustomfield */ public function addcustomfieldproperty(\bw\blogbundle\entity\customfieldproperty $customfieldproperties) { $this->customfieldproperties[] = $customfieldproperties; homecoming $this; } /** * remove customfieldproperties * * @param \bw\blogbundle\entity\customfieldproperty $customfieldproperties */ public function removecustomfieldproperty(\bw\blogbundle\entity\customfieldproperty $customfieldproperties) { $this->customfieldproperties->removeelement($customfieldproperties); } /** * customfieldproperties * * @return \doctrine\common\collections\collection */ public function getcustomfieldproperties() { homecoming $this->customfieldproperties; }

and have form type:

<?php namespace bw\blogbundle\form; utilize symfony\component\form\abstracttype; utilize symfony\component\form\formbuilderinterface; utilize symfony\component\optionsresolver\optionsresolverinterface; class postcustomfieldtype extends abstracttype { /** * @param formbuilderinterface $builder * @param array $options */ public function buildform(formbuilderinterface $builder, array $options) { $builder ->add('customfieldproperties', 'entity', array( 'class' => 'bw\blogbundle\entity\customfieldproperty', 'property' => 'name', 'multiple' => true, 'expanded' => true, ))

and work fine checkboxes, when seek utilize radio buttons (set 'multiple' => false,) - it's render them fine, when check , seek save entity db - have error:

neither property "customfieldproperties" nor 1 of methods "addcustomfieldproperty()"/"removecustomfieldproperty()", "setcustomfieldproperties()", "customfieldproperties()", "__set()" or "__call()" exist , have public access in class "bw\blogbundle\entity\postcustomfield".

why doctrine not save entity 'multiple' => false?

p.s. symfony standart edition v2.5.0

i had same problem own built entity , found possible solution on here.

based on code should add together function:

public function setcustomfieldproperties($customfieldproperties) { if (!is_array($customfieldproperties)) { $customfieldproperties= array($customfieldproperties); } $this->customfieldproperties= $customfieldproperties; }

this did trick me. indeed seems problem manytomany relation: setting multiple value false in formbuilder, appears aren't dealing array(collections) anymore (which expecting) , have convert array.

by way: function addcustomfieldproperty not used anymore in case.

i think it's improve avoid manytomany relation if possible, in case had maintain alternative open allow multiple entities later extensions.

symfony2 doctrine2 symfony-forms

No comments:

Post a Comment