从LazyLoadCollection获取元素 [英] Get elements from LazyLoadCollection

查看:101
本文介绍了从LazyLoadCollection获取元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果他们对我有用,我发现 Doctrine\Common\CollectionscCriteria 是一个非常有用的概念。

I have found Doctrine\Common\Collections\Criteria to be a very useful concept, if they worked for me.

在symfony控制器中,我将此代码称为:

In a symfony controller, I am calling this code:

$criteria = Criteria::create()
    ->where(Criteria::expr()->gt('position', 0))
    ->orderBy(['riskPosition', Criteria::ASC]);
$positions= $this->getDoctrine()->getRepository(DataCategory::class)->matching($criteria);

dump($positions->count()); // dumps 1, correct!
dump($positions);
foreach($positions as $r)
    dump($r); // -> Unrecognized field: 0

dump($ positions)

LazyCriteriaCollection {#881 ▼
  #entityPersister: JoinedSubclassPersister {#849 ▶}
  #criteria: Criteria {#848 ▼
    -expression: Comparison {#836 ▶}
    -orderings: array:2 [▶]
    -firstResult: null
    -maxResults: null
  }
  -count: 1
  #collection: null
  #initialized: false
}

一旦访问返回数组的元素,就会出现错误

As soon as I access an element of the returned array, I get an error

ORMException::unrecognizedField(0)
in vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php (line 1193)

但是一旦我想访问元素(例如循环和转储),我会遇到类似的错误。渲染期间引发了异常模板(无法识别的字段:0)。

But as soon as I want to access the elements (e.g. loop and dump) I get some error like An exception has been thrown during the rendering of a template ("Unrecognized field: 0").

据我研究的鳕鱼e,问题是尚未从数据库中获取查询结果。仅 count()有效。 如何触发该提取?

As far as I have studied the code, the problem is that the query results have not been fetched from the database. Only count() works. How can I trigger this fetch?

我的实体具有 @ ORM\InheritanceType( JOINED )

此代码(绕过使用标准)确实可以正确结果,但我想使用条件:

This code (circumventing the use of Criteria) does give correct results, but I'd like to use Criteria:

$riskPositions = $this->getDoctrine()->getRepository(DataCategory::class)
    ->createQueryBuilder('p')
    ->where('p.position > 0')
    ->orderBy('p.position', 'ASC')
    ->getQuery()
    ->execute();


推荐答案

问题是由以下行引起的:

The issue is caused by line:

->orderBy(['riskPosition', Criteria::ASC]);

Doctorrine\Common\CollectionsCriteria`s orderBy 接受数组参数,其中

Doctrine\Common\Collections\Criteria `s orderBy accepts an array argument where


键是字段,值是顺序,是ASC还是DESC。

Keys are field and values are the order, being either ASC or DESC.

github链接

显然,在学说s文档

因此,学说认为 0是数组参数的第一个键,是要排序的字段,但找不到

So doctrine thinks that "0", which is the 1st key of the array argument, is the field to sort by, but cannot find it.

要解决,请将上面的行更改为:

To solve, change the above line to:

->orderBy(['riskPosition' => Criteria::ASC]);

这篇关于从LazyLoadCollection获取元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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