entityManager保存并刷新

时间:2012-02-20 11:13:44

标签: symfony doctrine-orm entitymanager

我正在尝试保存级联某个对象并检索它。 我有3个对象超过3个实体。

的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;
...
}

实体上的对象:

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();
        }
    }
...
}

问题是,在我的测试中,当我尝试一个篮子添加BasketDeclination然后保存 篮子被保存,篮子也被淘汰。 然后,当我$ basket-&gt; refresh()时,篮子刷新,篮子从实体重建

但是当我有一个篮子的订单时,我添加了BasketDeclinaiton($ order-&gt; basket-&gt; addDeclination(...)) 当我保存所有实体时都会保存 然后当我刷新订单时,我收到订单和篮子。 但实体$ basket-&gt; getDeclinations()没有任何东西

我做错了什么?

3 个答案:

答案 0 :(得分:0)

我记得这来自doctrine1,不确定这里是否同样适用,在doctrine1刷新只刷新第一个直接对象,处理关系和东西你需要添加第二个参数deep = true来处理所有相关的也可以尝试查看类似的内容。

答案 1 :(得分:0)

如果问题确实是实体管理器不是refresh关联(由Mohammad AbuShady建议),则告诉您的实体级联refresh操作。

class Basket
{
    // ... 

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

    // ...
...
}

答案 2 :(得分:0)

我将进行两次猜测:

  • 您应该对类使用级联注释以允许级联&#34;持续&#34;并且可能是级联&#34;刷新&#34;太。正如documentation所述。类似的东西:

    @ORM \ OneToOne(targetEntity =&#34; Entity \ Order&#34;,mappedBy =&#34; basket&#34;,cascade = {&#34; persist&#34;,&#34; remove&# 34;,&#34;刷新&#34;})

  • 在刷新之前,您在Declinations上缺少persist()flush()。如果您没有级联,则需要在要保留的每个对象上调用它们,然后调用refresh()方法。

(在使用嵌套对象时,检查代理是否已创建并正常工作总是一个好主意)