5

I'm trying to save in cascade some object and retrieve it. I have 3 Object over 3 entities.

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 Entity:

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 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

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

What I am doing wrong?

Problematic
  • 17,567
  • 10
  • 73
  • 85
user1014102
  • 345
  • 2
  • 6
  • 17
  • 2
    This is a pure shot in the dark: try calling $entityManager()->clear() before doing the refresh. You have way too many objects and things going on for my poor brain to follow. You might also consider posting a very simple test case showing the problem. – Cerad Feb 20 '12 at 15:31
  • one point ...for `You have way too many objects and things going on for my poor brain to follow ` – Edwin O. Sep 07 '17 at 15:29

3 Answers3

1

If the problem is indeed that the entity manager is not refreshing associations (as suggested by Mohammad AbuShady), the answer to tell your entities to cascade the refresh operation.

class Basket
{
    // ... 

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

    // ...
...
}
Community
  • 1
  • 1
edan
  • 1,119
  • 1
  • 14
  • 13
1

I will go with two guesses:

  • You should use the cascade annotation to the classes to allow cascade "persist" and probably cascade "refresh" too. As stated on the documentation. Something like:

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

  • You are missing a persist() or a flush() on the Declinations before refreshing. If you are not cascading you need to call them on each object you want to keep and THEN call the refresh() method.

(It's always a good idea too to check that your proxies are created and working all right when you work with nested objects)

Hiraqui
  • 437
  • 3
  • 7
0

I remember this from doctrine1, not sure if the same applies here, in doctrine1 refreshing only refreshed the first direct object, to handle the relations and stuff you need to add a second parameter deep = true to handle all the related objects too, you can try looking into something like that.

Mohammad AbuShady
  • 40,884
  • 11
  • 78
  • 89
  • There is no second parameter for `EntityManager->refresh()` [method here](https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/EntityManager.php#L666) – Manse Jun 12 '13 at 16:59
  • 1
    ok things are a little confusing to me, but why don't you have a `$this->em->refresh($this->basket);` inside the `refresh` function – Mohammad AbuShady Jun 12 '13 at 17:40