如何让Doctrine 2返回一个实体而不是一个代理 [英] How to get Doctrine 2 to return an entity instead of a proxy

查看:120
本文介绍了如何让Doctrine 2返回一个实体而不是一个代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Doctrine 2实现深层复制功能,除了在我的一个实体上尝试在返回集合之前从关联中删除某些记录的方法,我几乎拥有它。

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.

问题是当我在下面调用getRoofAreas()时,我得到一个Proxy对象数组,我的深层拷贝代码不喜欢:

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.

有没有办法获取Proxy对象的Entity对象?

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

提前感谢任何帮助任何人都可以提供

Thanks in advance for any help anyone can provide

推荐答案

更改结果方法

return $q->getArrayResult();

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

这篇关于如何让Doctrine 2返回一个实体而不是一个代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆