doctrine2 - Symfony 2 - can't save form with collections -


i struggling symfony 2 form problem while. when trying save form getting following message:

a new entity found through relationship 'org\corebundle\entity\customlists#customlistelements' not configured cascade persist operations entity: org\corebundle\entity\customlistelement@000000007db26fee000000007e19f46a. solve issue: either explicitly call entitymanager#persist() on unknown entity or configure cascade persist association in mapping example @manytoone(..,cascade={"persist"}).

i have called persist() method on collection elements , added cascade={"persist"} inside entity, recommended in similar threads here, didn't help.

customlists.php:

 /**      * @var \doctrine\common\collections\collection      * @orm\onetomany(targetentity="customlistelement", mappedby="customlist", cascade={"persist"})      */     private $customlistelements;  /**      * add customlistelement      *      * @param \org\corebundle\entity\customlistelement $customlistelement      *      * @return customlists      */     public function addcustomlistelement(\org\corebundle\entity\customlistelement $customlistelement)     {         $this->customlistelements[] = $customlistelement;          return $this;     }      /**      * remove customlistelement      *      * @param \org\corebundle\entity\customlistelement $customlistelement      */     public function removecustomlistelement(\org\corebundle\entity\customlistelement $customlistelement)     {         $this->customlistelements->removeelement($customlistelement);     } 

customlistelement.php:

 /**      *       * @orm\manytoone(targetentity="customlists", inversedby="customlistelements", cascade={"persist"})      * @orm\joincolumn(name="custom_list", referencedcolumnname="id_custom_list", nullable=false)      */     private $customlist;    /**      * set customlist      *      * @param \org\corebundle\entity\customlists $customlist      *       * @return customlistelement      */      public function setcustomlist(\org\corebundle\entity\customlists $customlist = null)     {         $this->customlist = $customlist;          return $this;     } 

customlisttype.php:

    $builder->add('customlistelements', 'collection', array(             'type' => new customlistelementtype(),             'label' => false,             'allow_add' => true,             'allow_delete' => true,             'prototype' => true,             'attr' => array('class' => 'form-control')     )); 

contoller:

   if ($form->isvalid()) {         $data = $form->getdata();          $customlistelements = $data->getcustomlistelements();          foreach($customlistelements $element){             $element->setcustomlist($data);             $em->persist($element);             $em->flush();         }                 $em->persist($data);         $em->flush();     } 

i have no idea, doing wrong. thankful helpful answers.

edit: here form code:

{% extends 'organizerbundle::layout.html.twig' %}  {% block title %}{{ title }}{% endblock %}  {% block header %} {{ parent() }} {% endblock %}  {% block content %}     <div class="col col-md-8">         <div class="panel panel-success custom-list-form">           <div class="panel-heading">             <h3 class="panel-title">{{ title }}</h3>           </div>           <div class="panel-body">             {{ form_start(form) }}                  {{ form_row(form.listname) }}                  <h3>elementy</h3>                  <ul class="elements"                  data-prototype="{% filter escape %}{% include 'organizerbundle:customlists:elementtemplate.html.twig'  %}{% endfilter %}">                 {% tag in form.customlistelements %}                     <li>                     <div class="custom-list-element">                         <div id="customlist_customlistelements___name__">                             <div>                                  {{ form_row(tag.elementname) }}                             </div>                             <div>                                 {{ form_row(tag.elementdescription) }}                             </div>                             <input type="hidden" id="customlist_customlistelements___name___elementorder"                                 name="customlist[customlistelements][__name__][elementorder]" />                         </div>                         <div class="element-buttons">                             <i class="fa fa-times remove-element" title="{{ 'usuń'|trans }}" data-placement="left" data-toggle="tooltip"></i>                         </div>                          </div>                     </li>                 {% endfor %}                  </ul>                 {% form.customlistelements.setrendered %}                 <div class="form-buttons">{{ form_widget(form.zapisz) }}</div>             {{ form_end(form) }}           </div>         </div>     </div>     <script>     var $collectionholder;      var addelemtext = '{{ 'dodaj element'|trans }}';     var $newelementbutton = $('<a href="#" class="add_element_link"><button type="button" class="btn btn-primary btn-sm">'+              addelemtext +'</button></a>');     var $newlinkli = $('<li></li>');      jquery(document).ready(function() {          $collectionholder = $('.elements');          $newelementbutton.insertbefore($collectionholder);         $collectionholder.data('index', $collectionholder.find(':input').length);          $newelementbutton.on('click', function(e) {             e.preventdefault();             addelementform($collectionholder, $newlinkli);         });          function addelementform($collectionholder, $newlinkli) {              var prototype = $collectionholder.data('prototype');             var index = $collectionholder.data('index');             var newform = prototype.replace(/__name__/g, index);              $collectionholder.data('index', index + 1);              var $newformli = $('<li></li>').append(newform);             $collectionholder.append($newformli);         }      });     </script> {% endblock %} 

and elementtemplate.html.twig prototype template:

<div class="listbox" data-pk="{{ element.idcustomlist }}">     <input type="checkbox" class="list-checkbox"/>     <div class="custom-list-name">{{ element.listname }}</div>     <div class="list-elem-count">{{ 'elementów:'|trans ~ '...' }}</div>     <div class="list-elem-created">         <span class="created-text">{{ 'utworzono:'|trans }}</span>         <div class="date-created">{{ '...'  }}</div>     </div> </div> 

edit 2 : when doing doctrine:schema:validate, there error:

[mapping] fail - entity-class 'org\corebundle\entity\customlists' mapping invalid: * field org\corebundle\entity\customlists#customlistelements on inverse side of bi-directional relationship, specified mappedby association on target-entity org\corebundle\entity\customlistelement#customlist not contain required 'inversedby="customlistelements"' attribute.

did added line in constructor of customlists.php :

$customlistelements = new arraycollection(); 

change cascade in onetomany declaration.

/**      * @var \doctrine\common\collections\collection      * @orm\onetomany(targetentity="customlistelement", mappedby="customlist", cascade={"all"})      */     private $customlistelements; 

it not necessary read $customlistelements , add manually. doctrine automatically because know customlists had relationship customlistelement due onetomany.

you can change controller :

$entity = new customlists(); $form = $this->createform(new customlisttype(), $entity); $form->handlerequest($request);   if ($form->isvalid()) {        $em->persist($entity);        $em->flush();     } 

hope helps


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -