gpt4 book ai didi

doctrine-orm - 从 $query->setMaxResults() 获取奇怪的行为

转载 作者:行者123 更新时间:2023-12-04 01:52:34 26 4
gpt4 key购买 nike

当我在查询上调用 setMaxResults 时,它似乎想将最大数字视为“2”,无论它的实际值是多少。

function findMostRecentByOwnerUser(\Entities\User $user, $limit)
{
echo "2: $limit<br>";
$query = $this->getEntityManager()->createQuery('
SELECT t
FROM Entities\Thread t
JOIN t.messages m
JOIN t.group g
WHERE
g.ownerUser = :owner_user
ORDER BY m.timestamp DESC
');
$query->setParameter("owner_user", $user);
$query->setMaxResults(4);
echo $query->getSQL()."<br>";
$results = $query->getResult();
echo "3: ".count($results);
return $results;
}

当我注释掉 setMaxResults行,我得到 6 个结果。当我把它留在里面时,我会得到 2 个最近的结果。当我在 phpMyAdmin 中运行生成的 SQL 代码时,我得到了 4 个最近的结果。生成的SQL,供引用,为:
SELECT <lots of columns, all from t0_>
FROM Thread t0_
INNER JOIN Message m1_ ON t0_.id = m1_.thread_id
INNER JOIN Groups g2_ ON t0_.group_id = g2_.id
WHERE g2_.ownerUser_id = ?
ORDER BY m1_.timestamp DESC
LIMIT 4

编辑 :

阅读时 the DQL "Limit" documentation ,我遇到了以下情况:

If your query contains a fetch-joined collection specifying the result limit methods are not working as you would expect. Set Max Results restricts the number of database result rows, however in the case of fetch-joined collections one root entity might appear in many rows, effectively hydrating less than the specified number of results.



我很确定我不是在做一个 fetch-joined 集合。我的印象是 fetch-joined 集合是我做类似 SELECT t, m FROM Threads JOIN t.messages 的地方。 .我对此的理解不正确吗?

最佳答案

更新:使用 Doctrine 2.2+,您可以使用分页器 http://docs.doctrine-project.org/en/latest/tutorials/pagination.html

关于doctrine-orm - 从 $query->setMaxResults() 获取奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7289939/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com