php - Form collection data after handleRequest() is swapping -


i have embed collection of forms symfony tutorial. works, except existing entity removal.

initial data:

+----+-------+ | id | value | +----+-------+ | 1  |     | +----+-------+ | 2  | b     | +----+-------+ | 3  | c     | +----+-------+ 

then in collection form remove a value form. after submitting , handlerequest(), data became:

+----+-------+ | id | value | +----+-------+ | 1  | b     | +----+-------+ | 2  | c     | +----+-------+ 

looks handlerequest() overriding array collection values , removing diffs. expected result should be:

+----+-------+ | id | value | +----+-------+ | 2  | b     | +----+-------+ | 3  | c     | +----+-------+ 

so why happening?

controller:

public function indexaction(request $request, agreement $agreement) {     $form = $this         ->createform(costgroupscollectiontype::class, $agreement)         ->add('submit', submittype::class, ['label' => 'button.save']);     $form->handlerequest($request);      if ($form->isvalid()) {         $em = $this->getdoctrine()->getmanager();         $em->flush();          return $this->redirecttoroute('team_agreements_cost_groups', ['id' => $agreement->getid()]);     }      return array(         'form' => $form->createview(),         'agreement' => $agreement     ); } 

costgroupscollectiontype:

class costgroupscollectiontype extends abstracttype {     public function buildform(formbuilderinterface $builder, array $options)     {         $builder->add('costgroups', collectiontype::class, [             'label' => false,             'entry_type' => costgrouptype::class,             'cascade_validation' => true,             'allow_add'    => true,             'allow_delete' => true,             'prototype'    => true,             'by_reference' => false,             'attr'         => array(                 'class' => 'form-collection',             ),         ]);     }      public function configureoptions(optionsresolver $resolver)     {         $resolver->setdefaults([             'data_class' => 'nfq\teambundle\entity\agreement',             'cascade_validation' => true         ]);     }      public function getname()     {         return 'nfq_team_bundle_cost_groups_collection_type';     } } 

costgrouptype:

class costgrouptype extends abstracttype {     public function buildform(formbuilderinterface $builder, array $options)     {         /** @var employeerepository $employeerepo */         $employeerepo = $options['employeesrepo'];          $builder             ->add('costgroupposition', entitytype::class, [                 'label' => 'teams.cost_group.title',                 'class' => 'nfq\teambundle\entity\costgroupposition',                 'attr' => ['class' => 'autocomplete'],                 'choice_label' => 'title',                 'multiple' => false,                 'required' => true             ])             ->add('price', numbertype::class, [                 'label' => 'teams.cost_group.price',                 'required' => true             ])             ->add('employees', entitytype::class, [                 'label' => 'teams.cost_group.employees',                 'class'=> 'nfq\resourcesbundle\entity\employee',                 'attr' => ['class' => 'autocomplete-unique'],                 'choice_label' => function ($employee) {                     /** @var employee $employee */                     return $employee->getfullname();                 },                 'multiple'  => true,                 'required' => false             ])             ->add('currency', entitytype::class, [                 'label' => 'currency.single_title',                 'class' => currency::class,                 'choice_label' => function (currency $c) {                     return $c->getname();                 },                 'multiple' => false,                 'expanded' => false,                 'empty_data' => 'null',                 'placeholder' => 'misc.selectavalue',                 'required' => true             ])             ->add('exchangerate', numbertype::class, [                 'label' => 'teams.dedicated.invoices.details.exchange_rate',                 'required' => true,             ]);     }      public function configureoptions(optionsresolver $resolver)     {         $resolver->setdefaults([             'data_class' => 'nfq\teambundle\entity\costgroup'         ]);     }      public function getname()     {         return 'nfq_team_bundle_cost_group_type';     } } 

agreement

class agreement {     /**      * @var int      *      * @orm\column(name="id", type="integer")      * @orm\id      * @orm\generatedvalue(strategy="auto")      */     private $id;      /**      * @var string      *      * @orm\column(name="title", type="string", length=255)      */     private $title;      /**      * @orm\manytoone(targetentity="nfq\clientsbundle\entity\company", inversedby="agreements")      * @orm\joincolumn(name="company_id", referencedcolumnname="id")      */     private $client;      /**      * @orm\manytoone(targetentity="nfq\resourcesbundle\entity\team", inversedby="agreements")      * @orm\joincolumn(name="team_id", referencedcolumnname="id")      */     private $team;      /**      * @orm\manytoone(targetentity="nfq\userbundle\entity\user", inversedby="agreements")      * @orm\joincolumn(name="user_id", referencedcolumnname="id")      */     private $assignee;      /**      * @var \datetime      *      * @orm\column(name="start_date", type="date")      */     private $startdate;      /**      * @var \datetime      *      * @orm\column(name="end_date", type="date", nullable=true)      */     private $enddate;      /**      * @var bool      *      * @orm\column(name="ended", type="boolean")      */     private $ended;      /**      * @orm\onetoone(targetentity="nfq\teambundle\entity\requisites", mappedby="agreement")      */     private $requisites;      /**      * @orm\onetomany(targetentity="nfq\teambundle\entity\costgroup", mappedby="agreement",      *     cascade={"all"}, orphanremoval=true)      */     private $costgroups;      /**      * @orm\onetomany(targetentity="nfq\teambundle\entity\dedicatedinvoice", mappedby="agreement")      */     private $dedicatedinvoices;      /**      * constructor      */     public function __construct()     {         $this->ended = false;         $this->costgroups = new \doctrine\common\collections\arraycollection();         $this->dedicatedinvoices = new \doctrine\common\collections\arraycollection();     }      /**      * id.      *      * @return integer      */     public function getid()     {         return $this->id;     }      /**      * set title.      *      * @param string $title      *      * @return agreement      */     public function settitle($title)     {         $this->title = $title;          return $this;     }      /**      * title.      *      * @return string      */     public function gettitle()     {         return $this->title;     }      /**      * set startdate.      *      * @param \datetime $startdate      *      * @return agreement      */     public function setstartdate($startdate)     {         $this->startdate = $startdate;          return $this;     }      /**      * startdate.      *      * @return \datetime      */     public function getstartdate()     {         return $this->startdate;     }      /**      * set enddate.      *      * @param \datetime $enddate      *      * @return agreement      */     public function setenddate($enddate)     {         $this->enddate = $enddate;          return $this;     }      /**      * enddate.      *      * @return \datetime      */     public function getenddate()     {         return $this->enddate;     }      /**      * set ended.      *      * @param boolean $ended      *      * @return agreement      */     public function setended($ended)     {         $this->ended = $ended;          return $this;     }      /**      * ended.      *      * @return boolean      */     public function getended()     {         return $this->ended;     }      /**      * set client.      *      * @param \nfq\clientsbundle\entity\company $client      *      * @return agreement      */     public function setclient(\nfq\clientsbundle\entity\company $client = null)     {         $this->client = $client;          return $this;     }      /**      * client.      *      * @return \nfq\clientsbundle\entity\company      */     public function getclient()     {         return $this->client;     }      /**      * set team.      *      * @param \nfq\resourcesbundle\entity\team $team      *      * @return agreement      */     public function setteam(\nfq\resourcesbundle\entity\team $team = null)     {         $this->team = $team;          return $this;     }      /**      * team.      *      * @return \nfq\resourcesbundle\entity\team      */     public function getteam()     {         return $this->team;     }      /**      * set assignee.      *      * @param user $assignee      *      * @return agreement      */     public function setassignee(user $assignee = null)     {         $this->assignee = $assignee;          return $this;     }      /**      * assignee.      *      * @return user      */     public function getassignee()     {         return $this->assignee;     }      /**      * set requisites.      *      * @param \nfq\teambundle\entity\requisites $requisites      *      * @return agreement      */     public function setrequisites(\nfq\teambundle\entity\requisites $requisites = null)     {         $this->requisites = $requisites;          return $this;     }      /**      * requisites.      *      * @return \nfq\teambundle\entity\requisites      */     public function getrequisites()     {         return $this->requisites;     }      /**      * add costgroup.      *      * @param \nfq\teambundle\entity\costgroup $costgroup      *      * @return agreement      */     public function addcostgroup(\nfq\teambundle\entity\costgroup $costgroup)     {         if (!$this->costgroups->contains($costgroup)) {             $this->costgroups->add($costgroup);         }         $costgroup->setagreement($this);          return $this;     }      /**      * remove costgroup.      *      * @param \nfq\teambundle\entity\costgroup $costgroup      */     public function removecostgroup(\nfq\teambundle\entity\costgroup $costgroup)     {         if ($this->costgroups->contains($costgroup)) {             $this->costgroups->removeelement($costgroup);         }         $costgroup->setagreement(null);     }      /**      * costgroups.      *      * @return \doctrine\common\collections\collection      */     public function getcostgroups()     {         return $this->costgroups;     }      /**      * add dedicatedinvoice.      *      * @param \nfq\teambundle\entity\dedicatedinvoice $dedicatedinvoice      *      * @return agreement      */     public function adddedicatedinvoice(\nfq\teambundle\entity\dedicatedinvoice $dedicatedinvoice)     {         $this->dedicatedinvoices[] = $dedicatedinvoice;          return $this;     }      /**      * remove dedicatedinvoice.      *      * @param \nfq\teambundle\entity\dedicatedinvoice $dedicatedinvoice      */     public function removededicatedinvoice(\nfq\teambundle\entity\dedicatedinvoice $dedicatedinvoice)     {         $this->dedicatedinvoices->removeelement($dedicatedinvoice);     }      /**      * dedicatedinvoices.      *      * @return \doctrine\common\collections\collection      */     public function getdedicatedinvoices()     {         return $this->dedicatedinvoices;     } } 

costgroup

class costgroup {     /**      * @var int      *      * @orm\column(name="id", type="integer")      * @orm\id      * @orm\generatedvalue(strategy="auto")      */     private $id;      /**      * @var float      *      * @orm\column(name="price", type="decimal", precision=10, scale=2)      */     private $price;      /**      * @orm\manytoone(targetentity="nfq\teambundle\entity\agreement", inversedby="costgroups")      * @orm\joincolumn(name="agreement_id", referencedcolumnname="id", ondelete="cascade")      */     private $agreement;      /**      * @orm\manytoone(targetentity="nfq\teambundle\entity\costgroupposition", inversedby="costgroups")      * @orm\joincolumn(name="cost_group_position_id", referencedcolumnname="id", ondelete="cascade")      */     private $costgroupposition;      /**      * @orm\manytomany(targetentity="nfq\resourcesbundle\entity\employee", inversedby="costgroups")      * @orm\jointable(name="cost_group_employee")      */     private $employees;      /**      * @orm\onetomany(targetentity="nfq\teambundle\entity\dedicatedemployees", mappedby="costgroup")      */     private $dedicatedemployees;      /**      * @var float      * @orm\column(name="exchange_rate", type="float", nullable=true)      */     private $exchangerate;      /**      * @var currency      * @orm\manytoone(targetentity="nfq\settingsbundle\entity\currency", inversedby="costgroups")      * @orm\joincolumn(name="currency_id", referencedcolumnname="id")      */     private $currency;      /**      * constructor.      */     public function __construct()     {         $this->employees = new \doctrine\common\collections\arraycollection();         $this->dedicatedemployees = new \doctrine\common\collections\arraycollection();         $this->exchangerate = 1;     }      /**      * id.      *      * @return integer      */     public function getid()     {         return $this->id;     }      /**      * set price.      *      * @param string $price      *      * @return costgroup      */     public function setprice($price)     {         $this->price = $price;          return $this;     }      /**      * price.      *      * @return string      */     public function getprice()     {         return $this->price;     }      /**      * set agreement.      *      * @param \nfq\teambundle\entity\agreement $agreement      *      * @return costgroup      */     public function setagreement(\nfq\teambundle\entity\agreement $agreement = null)     {         $this->agreement = $agreement;          return $this;     }      /**      * agreement.      *      * @return \nfq\teambundle\entity\agreement      */     public function getagreement()     {         return $this->agreement;     }      /**      * set costgroupposition.      *      * @param \nfq\teambundle\entity\costgroupposition $costgroupposition      *      * @return costgroup      */     public function setcostgroupposition(\nfq\teambundle\entity\costgroupposition $costgroupposition = null)     {         $this->costgroupposition = $costgroupposition;          return $this;     }      /**      * costgroupposition.      *      * @return \nfq\teambundle\entity\costgroupposition      */     public function getcostgroupposition()     {         return $this->costgroupposition;     }      /**      * add employee.      *      * @param \nfq\resourcesbundle\entity\employee $employee      *      * @return costgroup      */     public function addemployee(\nfq\resourcesbundle\entity\employee $employee)     {         $this->employees[] = $employee;          return $this;     }      /**      * remove employee.      *      * @param \nfq\resourcesbundle\entity\employee $employee      */     public function removeemployee(\nfq\resourcesbundle\entity\employee $employee)     {         $this->employees->removeelement($employee);     }      /**      * employees.      *      * @return \doctrine\common\collections\collection      */     public function getemployees()     {         return $this->employees;     }      /**      * add dedicatedemployee.      *      * @param \nfq\teambundle\entity\dedicatedemployees $dedicatedemployee      *      * @return costgroup      */     public function adddedicatedemployee(\nfq\teambundle\entity\dedicatedemployees $dedicatedemployee)     {         $this->dedicatedemployees[] = $dedicatedemployee;          return $this;     }      /**      * remove dedicatedemployee.      *      * @param \nfq\teambundle\entity\dedicatedemployees $dedicatedemployee      */     public function removededicatedemployee(\nfq\teambundle\entity\dedicatedemployees $dedicatedemployee)     {         $this->dedicatedemployees->removeelement($dedicatedemployee);     }      /**      * dedicatedemployees.      *      * @return \doctrine\common\collections\collection      */     public function getdedicatedemployees()     {         return $this->dedicatedemployees;     }      /**      * set exchangerate.      *      * @param float $exchangerate      *      * @return costgroup      */     public function setexchangerate($exchangerate)     {         $this->exchangerate = $exchangerate;          return $this;     }      /**      * exchangerate.      *      * @return float      */     public function getexchangerate()     {         return $this->exchangerate;     }      /**      * set currency.      *      * @param currency $currency      *      * @return costgroup      */     public function setcurrency(currency $currency = null)     {         $this->currency = $currency;          return $this;     }      /**      * currency.      *      * @return currency      */     public function getcurrency()     {         return $this->currency;     } } 

see the documentation how handle correctly persisting of arraycollection doctrine.


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -