开发者

How to get Doctrine 2 to return an entity instead of a proxy

I'm trying to implement deep copy functionality using Doctrine 2, and I almost have it except for a method on one of my entities that attempts to strip out certain records from an association before returning the collection.

The problem is that when I call getRoofAreas() below, I get an array of Proxy objects, which my deep copy code doesn't like:

/**
 * @OneToMany(targetEntity="\Entities\QuotingRoofAreas", mappedBy="customerId", cascade={"persist"})
 * @OrderBy({"areaIndex" = "ASC"})
 */

private $ro开发者_运维知识库ofAreas;

public function getRoofAreas() {
    $em = \Zend_Registry::get('em');
    $q = $em->createQuery("select ra from \Entities\QuotingRoofAreas ra where ra.dateDeleted IS NULL and ra.customerId = " . $this->getId());
    return $q->getResult();
}

but if I were to change this to:

/**
 * @OneToMany(targetEntity="\Entities\QuotingRoofAreas", mappedBy="customerId", cascade={"persist"})
 * @OrderBy({"areaIndex" = "ASC"})
 */

private $roofAreas;

public function getRoofAreas() {
    return $roofAreas;
}

then it would return a persistent collection which, when iterated through, would get me Entity objects, which is what I want. The latter approach doesn't strip out deleted roof areas, which is a must for my use case.

Is there a way to get the Entity object for a Proxy object?

Thanks in advance for any help anyone can provide


Change your results method

return $q->getArrayResult();

http://www.doctrine-project.org/docs/orm/2.0/en/reference/dql-doctrine-query-language.html#array-hydration

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜