symfony - Updated entity adds new row to database -


i trying update entities using ajax adds new row instead update existing one.

my entities:

  • category
  • product
  • subcategory

relations:

category manytomany product

category onetomany subcategory

category entity

 /**      * @orm\manytomany(targetentity="test\corebundle\entity\product", inversedby="categories", cascade={"persist"})      * @groups({"public", "admin"})      */     protected $products;    public function setproducts( $products) {         $this->products= new arraycollection($products->toarray());         return $this;     } 

product entity

  /**      * @orm\manytomany(targetentity="test\corebundle\entity\category", mappedby="products" )      * @exclude()      */     protected $categories; 

controller

 $tosave  = $this->get('request')->getcontent();         $s =  $serializer->deserialize($tosave, test\corebundle\entity\category, 'json');                   if ($s->getid() > 0) {                       $category= $em->getrepository('testcorebundle:category)->findoneby(['id' => $s->getid()]);                      $category->setname($s->getname());                     $category->setisdisabled($s->getisdisabled());                     $category->setisprivate($s->getisprivate());                         $category->setquestions($s->getcategories());                      $em->persist($category);                     $em->flush();                 } 

problem:

this code adds new products database, want update existing records.

things tried:

  • replaced persist merge, made no difference.
  • removed persist, same result

i solved problem following way:

1) replaced persist merge.

  $em->merge($category);   $em->flush(); 

2) changed cascade option persist merge

/**      * @orm\manytomany(targetentity="test\corebundle\entity\product", inversedby="categories", cascade={"merge"})      * @groups({"public", "admin"})      */     protected $products; 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -