【发布时间】:2014-11-06 02:17:24
【问题描述】:
我正在尝试将 Yii2 中的活动数据提供者的分页限制设置为 5 条记录。根据THIS 文档, $limit 属性可用,但我收到此错误:
Setting read-only property: yii\data\Pagination::limit
我的代码是这样的:
$dataProvider = new ActiveDataProvider([
'query' => Order::find()
->where(['user_id' => $user_id]),
'sort' =>[
'defaultOrder' => [
'id' => SORT_DESC
]
],
'pagination' => [
'pageSize' => 20,
'limit' => 5,
],
]);
我的问题是:为什么会发生这种情况?如果我删除 limit 一切顺利,pageSize 就可以工作...在 limit 上,一切都失败了...我该如何解决这个问题?
L.E:我找到了引发此错误的源方法,以防万一:
/**
* Sets value of an object property.
*
* Do not call this method directly as it is a PHP magic method that
* will be implicitly called when executing `$object->property = $value;`.
* @param string $name the property name or the event name
* @param mixed $value the property value
* @throws UnknownPropertyException if the property is not defined
* @throws InvalidCallException if the property is read-only
* @see __get()
*/
public function __set($name, $value)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
$this->$setter($value);
} elseif (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
} else {
throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
}
}
据此,我正在尝试:getLimit() 而不是setLimit()...任何想法为什么?
【问题讨论】:
-
你想达到什么目的?
pageSize将决定每页显示多少行。 -
@Alex 我试图只显示我的数据库中的最后 5 条记录。我看到了文档,我认为我在分页中有可用的 limit 属性,但事实证明,我没有......检查我的答案
-
我对这个问题投了反对票:1) 从代码中可以清楚地看出 getLimit 是 getPageSize 方法的别名,用于 SQL 数据检索(例如 MySQL 中没有 pageSize ,但是有一个限制)。 2) 设置只读属性的原因是什么?作者明确设置这是只读的,您不应该手动设置它。 3)“使用源卢克”——只需打开源代码并在代码中找到答案,而不是问愚蠢的问题“为什么我不能设置只读属性”。
标签: php pagination yii2