Note that there are some explanatory texts on larger screens.

plurals
  1. POentityManager save and refresh
    text
    copied!<p>I'm trying to save in cascade some object and retrieve it. I have 3 Object over 3 entities.</p> <p>Entites:</p> <pre><code>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; ... } </code></pre> <p>Object Over Entity:</p> <pre><code>class OrderObject { function __construct( EntityManager $em, Order $entity = null, BasketObject $basket = null ) { $this-&gt;em = $em; if (!$entity) { $this-&gt;entity = new Order(); $this-&gt;basket = $basket; } else { $this-&gt;setDataFromEntity($entity); } } protected function setDataFromEntity(Order $entity) { $basketFactory = new BasketFactory($this-&gt;em); $this-&gt;entity = $entity; $this-&gt;basket = $basketFactory-&gt;getBasket($entity-&gt;getBasket()-&gt;getId()); } public function save($flush = false) { // save subObject $this-&gt;basket-&gt;save(); // set link $this-&gt;entity-&gt;setBasket($this-&gt;basket-&gt;getEntity()); $this-&gt;em-&gt;persist($this-&gt;entity); if ($flush) { $this-&gt;em-&gt;flush(); } } public function refresh() { $this-&gt;em-&gt;refresh($this-&gt;entity); $this-&gt;setDataFromEntity($this-&gt;entity); } ... } class BasketObject { function __construct(EntityManager $em, Basket $entity = null) { $this-&gt;em = $em; if (!$entity) { $this-&gt;entity = new Basket(); $this-&gt;declinations = array(); } else { $this-&gt;setDataFromEntity($entity); } } protected function setDataFromEntity(Basket $entity) { $this-&gt;entity = $entity; $this-&gt;declinations = array(); foreach ($entity-&gt;getDeclinations() as $declination) { $this-&gt;declinations[] = new BasketDeclinationObject($this-&gt;em, $declination); } } public function save($flush = false) { foreach ($this-&gt;declinations as $declination) { $declination-&gt;save(); } $this-&gt;em-&gt;persist($this-&gt;entity); if ($flush) { $this-&gt;em-&gt;flush(); } } ... } class BasketDeclinationObject { public function __construct( EntityManager $em, BasketDeclination $entity= null, BasketObject $basket = null) { $this-&gt;em = $em; if (!$entity) { $this-&gt;entity = new BasketDeclination(); $this-&gt;basket = $basket; } else { $this-&gt;setDataFromEntity($entity); } } protected function setDataFromEntity(BasketDeclination $entity) { $this-&gt;entity = $entity; $declinationFactory = new DeclinationFactory($this-&gt;em); $this-&gt;declination = $declinationFactory-&gt;getDeclination($entity-&gt;getDeclination()-&gt;getId()); } public function save($flush = false) { if ($this-&gt;quantity &lt;= 0) { $this-&gt;em-&gt;remove($this-&gt;entity); $this-&gt;remove = true; return ; } if (!$this-&gt;entity-&gt;getId()) { $this-&gt;entity-&gt;setBasket($this-&gt;basket-&gt;getEntity()); } $this-&gt;entity-&gt;setQuantity($this-&gt;quantity); $this-&gt;em-&gt;persist($this-&gt;entity); if ($flush) { $this-&gt;em-&gt;flush(); } } ... } </code></pre> <p>The problem is that in my test when I try for a basket to add BasketDeclination then save the Basket is saved and BasketDeclination too. Then when I $basket->refresh() the basket is refresh and the BasketDeclinaiton is rebuild from entity</p> <p>BUT when I have an order whith a basket and I add BasketDeclinaiton ($order->basket->addDeclination(...)) When I save all entities are saved then when I refresh the order I get back the order and the basket. but the entity $basket->getDeclinations() does not have any thing</p> <p>What I am doing wrong? </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload