Propel 没有返回所有行
Propel is not returning all the rows
我正在尝试使用 propel 执行查询,它应该 return 50 行数据,但不是 return 其他随机行数。我打印了在我的 pgphpadmin 中复制的查询,它 return 有 50 行。我不确定发生了什么。此外,如果我在 "find" 之前执行 "count" 函数,它 returns 50,但在它之后 returns 随机行数。
$limit = 50;
$offset = 0;
..... //filters
$companies = $companies->orderById()->limit($limit)->offset($offset);
var_dump($companies->count()); // this returns 50
$companies = $companies->find();
var_dump($companies->count()); // this returns 13
同样foreach after只迭代了13次
这是生成的查询
SELECT "company"."id", "company"."otherfields",
"lists"."id", "lists"."otherfields", "place"."id",
"place"."otherfields", "contact"."id", "contact"."otherfields",
"entry"."id","entry"."otherfields"
FROM "company" LEFT JOIN "lists" ON
("company"."sector_id"="lists"."id") LEFT JOIN "place" ON
("company"."country_id"="place"."id") LEFT JOIN "contact" ON
("company"."id"="contact"."company_id" AND "contact"."active" = true)
LEFT JOIN "entry" ON ("company"."id"="entry"."company_id")
WHERE "company"."active"=true ORDER BY "company"."id" ASC LIMIT 50 OFFSET 0
Propel 版本为 2.0-dev
我一直对这个查询有疑问,所以我认为问题可能来自于我在库中删除了一个 throw。我正在使用 with() 和 limit()。生成的查询没问题,但结果不行。
已更改 Propel/Runtime/Formatter/ObjectFormatter。php
我已经评论过了。
/*if ($this->hasLimit) {
throw new LogicException('Cannot use limit() in conjunction with with() on a one-to-many relationship. Please remove the with() call, or the limit() call.');
}*/
我想错误可能来自这里。
我的加入查询如下所示
$companies->leftJoinWithLists()->leftJoinWithPlace()->leftJoinWithContact()->addJoinCondition('Contact','Contact.active = ?', true)->leftJoinWithEntry();
然后我加上限制。有什么办法可以避免这种情况或采取其他方式吗?
Any way to avoid this or do it another way?
你需要换一种方式。请参阅我对您在 https://github.com/propelorm/Propel2/issues/1231
上创建的问题的评论
基本上,SQL 中的 LIMIT
子句将限制结果集中的行数,但由于您使用的是 LEFT JOIN
,一个公司实体可能表示为结果集的多行(请参阅 pgphpadmin 中的原始结果)。
解决方法:使用多个查询。我建议首先查询您想要的所有公司,然后在结果 ObjectCollection
.
上使用 ->populateRelation()
方法
我正在尝试使用 propel 执行查询,它应该 return 50 行数据,但不是 return 其他随机行数。我打印了在我的 pgphpadmin 中复制的查询,它 return 有 50 行。我不确定发生了什么。此外,如果我在 "find" 之前执行 "count" 函数,它 returns 50,但在它之后 returns 随机行数。
$limit = 50;
$offset = 0;
..... //filters
$companies = $companies->orderById()->limit($limit)->offset($offset);
var_dump($companies->count()); // this returns 50
$companies = $companies->find();
var_dump($companies->count()); // this returns 13
同样foreach after只迭代了13次
这是生成的查询
SELECT "company"."id", "company"."otherfields",
"lists"."id", "lists"."otherfields", "place"."id",
"place"."otherfields", "contact"."id", "contact"."otherfields",
"entry"."id","entry"."otherfields"
FROM "company" LEFT JOIN "lists" ON
("company"."sector_id"="lists"."id") LEFT JOIN "place" ON
("company"."country_id"="place"."id") LEFT JOIN "contact" ON
("company"."id"="contact"."company_id" AND "contact"."active" = true)
LEFT JOIN "entry" ON ("company"."id"="entry"."company_id")
WHERE "company"."active"=true ORDER BY "company"."id" ASC LIMIT 50 OFFSET 0
Propel 版本为 2.0-dev
我一直对这个查询有疑问,所以我认为问题可能来自于我在库中删除了一个 throw。我正在使用 with() 和 limit()。生成的查询没问题,但结果不行。
已更改 Propel/Runtime/Formatter/ObjectFormatter。php 我已经评论过了。
/*if ($this->hasLimit) {
throw new LogicException('Cannot use limit() in conjunction with with() on a one-to-many relationship. Please remove the with() call, or the limit() call.');
}*/
我想错误可能来自这里。
我的加入查询如下所示
$companies->leftJoinWithLists()->leftJoinWithPlace()->leftJoinWithContact()->addJoinCondition('Contact','Contact.active = ?', true)->leftJoinWithEntry();
然后我加上限制。有什么办法可以避免这种情况或采取其他方式吗?
Any way to avoid this or do it another way?
你需要换一种方式。请参阅我对您在 https://github.com/propelorm/Propel2/issues/1231
上创建的问题的评论基本上,SQL 中的 LIMIT
子句将限制结果集中的行数,但由于您使用的是 LEFT JOIN
,一个公司实体可能表示为结果集的多行(请参阅 pgphpadmin 中的原始结果)。
解决方法:使用多个查询。我建议首先查询您想要的所有公司,然后在结果 ObjectCollection
.
->populateRelation()
方法