Retain and update entityManager

I am trying to save some object in a cascade and get it. I have 3 objects over 3 objects.

Entites:

class Order { /** * @var integer $id * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @var object $basket * * @ORM\OneToOne(targetEntity="Entity\Basket", inversedBy="order") */ protected $basket; ... } class Basket { /** * @var integer $id * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @var array $declinations * * @ORM\OneToMany(targetEntity="Entity\BasketDeclination", mappedBy="basket") */ protected $declinations; /** * Order owner (reversed side) * * @var OrderClient $order * * @ORM\OneToOne(targetEntity="Entity\Order", mappedBy="basket") */ protected $order; ... } class BasketDeclination { /** * @var integer $id * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @var integer $basket * * @ORM\ManyToOne(targetEntity="Entity\Basket", inversedBy="declinations") */ protected $basket; ... } 

Object over object:

 class OrderObject { function __construct( EntityManager $em, Order $entity = null, BasketObject $basket = null ) { $this->em = $em; if (!$entity) { $this->entity = new Order(); $this->basket = $basket; } else { $this->setDataFromEntity($entity); } } protected function setDataFromEntity(Order $entity) { $basketFactory = new BasketFactory($this->em); $this->entity = $entity; $this->basket = $basketFactory->getBasket($entity->getBasket()->getId()); } public function save($flush = false) { // save subObject $this->basket->save(); // set link $this->entity->setBasket($this->basket->getEntity()); $this->em->persist($this->entity); if ($flush) { $this->em->flush(); } } public function refresh() { $this->em->refresh($this->entity); $this->setDataFromEntity($this->entity); } ... } class BasketObject { function __construct(EntityManager $em, Basket $entity = null) { $this->em = $em; if (!$entity) { $this->entity = new Basket(); $this->declinations = array(); } else { $this->setDataFromEntity($entity); } } protected function setDataFromEntity(Basket $entity) { $this->entity = $entity; $this->declinations = array(); foreach ($entity->getDeclinations() as $declination) { $this->declinations[] = new BasketDeclinationObject($this->em, $declination); } } public function save($flush = false) { foreach ($this->declinations as $declination) { $declination->save(); } $this->em->persist($this->entity); if ($flush) { $this->em->flush(); } } ... } class BasketDeclinationObject { public function __construct( EntityManager $em, BasketDeclination $entity= null, BasketObject $basket = null) { $this->em = $em; if (!$entity) { $this->entity = new BasketDeclination(); $this->basket = $basket; } else { $this->setDataFromEntity($entity); } } protected function setDataFromEntity(BasketDeclination $entity) { $this->entity = $entity; $declinationFactory = new DeclinationFactory($this->em); $this->declination = $declinationFactory->getDeclination($entity->getDeclination()->getId()); } public function save($flush = false) { if ($this->quantity <= 0) { $this->em->remove($this->entity); $this->remove = true; return ; } if (!$this->entity->getId()) { $this->entity->setBasket($this->basket->getEntity()); } $this->entity->setQuantity($this->quantity); $this->em->persist($this->entity); if ($flush) { $this->em->flush(); } } ... } 

The problem is that in my test, when I try to add the BasketDeclination basket, the Basket is saved and BasketDeclination too. Then, when I $ basket-> refresh () the basket is updated, and BasketDeclinaiton is restored from the entity

BUT, when I have an order with a basket, and I add BasketDeclinaiton ($ order-> basket-> addDeclination (...)) When I save all entities, they are saved then when I update the order, I return the order and basket. but the essence of $ basket-> getDeclinations () has nothing

What am I doing wrong?

+4
source share
3 answers

I remember this from doctrine1, I’m not sure that the same applies here, in the doctrine1 update only the first direct object is updated to handle relations and stuff, you need to add a second parameter deep = true to process all related objects, you can try something like that.

0
source

If the problem is really that the manager of the object is not a refresh ING association (as suggested by Mohammad AbuShady ), the answer is to tell your subjects in the refresh cascade.

 class Basket { // ... /** * @var array $declinations * * @ORM\OneToMany( * targetEntity="Entity\BasketDeclination", * mappedBy="basket", * cascade={"refresh"} // THIS LINE ADDED * ) */ protected $declinations; // ... ... } 
0
source

I will go with two guesses:

  • You should use cascading class annotation to enable cascading "persist" and possibly cascading "updating". As indicated in the documentation . Sort of:

    @ORM \ OneToOne (targetEntity = "Entity \ Order", mappedBy = "trash", cascade = {"persist", "remove", "refresh"})

  • You are missing persist() or flush() on Declinations before upgrading. If you are not cascading, you need to call them on every object that you want to save, and THEN call the refresh() method.

(It is always a good idea to check that your proxies are created and working correctly when you work with nested objects)

0
source

Source: https://habr.com/ru/post/1397413/


All Articles