I am trying to save some object in a cascade and get it. I have 3 objects over 3 objects.
Entites:
class Order { protected $id; protected $basket; ... } class Basket { protected $id; protected $declinations; protected $order; ... } class BasketDeclination { protected $id; 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?
source share