Q
Q
Qwyu_4iLofK2022-04-07 21:56:30
Doctrine ORM
Qwyu_4iLofK, 2022-04-07 21:56:30

How to fix cascade persist?

I have two entities

class Orders{

   /**
     * @ORM\OneToMany(targetEntity=Positions::class, mappedBy="ord",cascade={ "remove", 
"persist"})
     */
    private $positions;
}


class Positions{
 /**
     * @ORM\ManyToOne(targetEntity=Orders::class, inversedBy="positions")
     * @ORM\JoinColumn(name="ord",referencedColumnName="guid",onDelete="CASCADE")
     */
    private $ord;
}


when trying to delete an order

class OrdersRepository{
public function delOrder(){
   ....
   $this->em->remove($order);
   $this->em->flush();
}
}

, an error occurs:

A new entity was found through the relationship 'App\Entity\Positions#ord' that was not configured to cascade persist operations for
entity: App\Entity\[email protected] To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist
this association in the mapping for example @ManyToOne(..,cascade={"persist"}). If you cannot find out which entity causes the problem implement '
App\Entity\Orders#__toString()' to get a clue.


I have it, but it doesn’t work ...

As soon as I put it in the order deletion function:
class OrdersRepository{
public function delOrder(){
   ....
   $this->em->remove($order);
   $this->em->flush();
   $this->em->clear(); //?????? верно ли, почему  помогло?
}
}

the error disappears, and why? honestly, I spied the answer there , but did not understand why, and is it true?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question