返回记录数,不考虑 Limit cakePHP 3
Count of no of record returned, without considering Limit cakePHP 3
我想要当前查询的数据库中可用的记录数,但不考虑 LIMIT。
$this->Orders->find('all')
->where(['order_quantity']=>5)
->LIMIT(5);
考虑一下,对于上述查询,我有 50 条记录。所以只想要当前查询可用的记录数。我不能使用 'count()' 因为限制它总是 return 可用记录总数小于或等于 5。cakePHP 中有任何解决方案吗?
我认为您不熟悉 find 中 limit 的使用。所以,我建议你研究docs.
您的查询中的限制意味着即使查询实际有 50 条数据,该查询也只会显示前 5 条数据。
因此,为了获得实际数据,您只需取消限制并在代码中进行一些更改,如下所示:
$this->Orders->find('count')->where(['order_quantity' => 5]);
This page in the CakePHP 3 book,准确解释您的问题的答案,包括其工作原理和原因:
Returning the Total Count of Records
Using a single query object, it is possible to obtain the total number
of rows found for a set of conditions:
$total = $articles->find()->where(['is_active' => true])->count();
The count() method will ignore the limit, offset and page clauses,
thus the following will return the same result:
$total = $articles->find()->where(['is_active' => true])->limit(10)->count();
This is useful when you need to know the total result set size in
advance, without having to construct another Query object. Likewise,
all result formatting and map-reduce routines are ignored when using
the count() method.
注意关于“...将忽略限制、偏移和页面子句”的内容
所以尝试这样的事情:
$data = $articles->find()->where(['is_active' => true])->limit(10);
$count = $data->count();
我想要当前查询的数据库中可用的记录数,但不考虑 LIMIT。
$this->Orders->find('all')
->where(['order_quantity']=>5)
->LIMIT(5);
考虑一下,对于上述查询,我有 50 条记录。所以只想要当前查询可用的记录数。我不能使用 'count()' 因为限制它总是 return 可用记录总数小于或等于 5。cakePHP 中有任何解决方案吗?
我认为您不熟悉 find 中 limit 的使用。所以,我建议你研究docs.
您的查询中的限制意味着即使查询实际有 50 条数据,该查询也只会显示前 5 条数据。
因此,为了获得实际数据,您只需取消限制并在代码中进行一些更改,如下所示:
$this->Orders->find('count')->where(['order_quantity' => 5]);
This page in the CakePHP 3 book,准确解释您的问题的答案,包括其工作原理和原因:
Returning the Total Count of Records
Using a single query object, it is possible to obtain the total number of rows found for a set of conditions:
$total = $articles->find()->where(['is_active' => true])->count();
The count() method will ignore the limit, offset and page clauses, thus the following will return the same result:
$total = $articles->find()->where(['is_active' => true])->limit(10)->count();
This is useful when you need to know the total result set size in advance, without having to construct another Query object. Likewise, all result formatting and map-reduce routines are ignored when using the count() method.
注意关于“...将忽略限制、偏移和页面子句”的内容
所以尝试这样的事情:
$data = $articles->find()->where(['is_active' => true])->limit(10);
$count = $data->count();