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

断了今生、忘了曾经 提交于 2019-12-10 17:24:48

问题


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 $roofAreas;

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


回答1:


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



来源:https://stackoverflow.com/questions/6168963/how-to-get-doctrine-2-to-return-an-entity-instead-of-a-proxy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!