CakePHP 3.0中的分页
我一直在玩CakePHP3.0的开发者预览版,我一直在尝试让新的ORM使用分页 在我的PostsController.php中,我有:CakePHP 3.0中的分页,php,cakephp,pagination,cakephp-3.0,Php,Cakephp,Pagination,Cakephp 3.0,我一直在玩CakePHP3.0的开发者预览版,我一直在尝试让新的ORM使用分页 在我的PostsController.php中,我有: <?php namespace App\Controller; use App\Controller\AppController; class PostsController extends AppController { public $name = 'Posts'; public $uses = 'Posts'; p
<?php
namespace App\Controller;
use App\Controller\AppController;
class PostsController extends AppController {
public $name = 'Posts';
public $uses = 'Posts';
public $components = ['Paginator'];
public $paginate = [
'fields' => ['Posts.id'],
'limit' => 1,
'order' => [
'Post.id' => 'asc'
]
];
public function index() {
$posts = $this->paginate('Posts');
$this->set('posts', $posts);
}
}
正如你所看到的,这是一个巨大的物体,数据大概就在其中的某个地方
显然这是因为数据不是直接返回到新的
但是一个物体
这不是一个bug,而是一个特性CakePHP3返回您可以看到的对象,并返回记录的实体对象。您现在必须使用这些对象,而不是数组
如果你真的阅读了你链接的迁移指南,我会很伤心,因为它就在那里:
- Cake\ORM\ResultSet-一组结果,为聚合中操作数据提供了强大的工具
- Cake\ORM\Entity-表示单行结果。使访问数据和序列化为各种格式成为一个简单的过程
public function index() {
$this->set('users', $this->Paginator->paginate($this->Users, [
'limit' => 5,
'conditions' => [
'Users.active' => 1
]
]));
}
paginate()方法的doc块中有一个
View index.ctp:
foreach ($users as $user) {
debug($user);
}
这将显示实体对象。我不是在这里粘贴整个长调试输出,只是其中的一部分
object(Cake\ORM\Entity) {
[protected] _properties => array(
'password' => '*****',
'id' => '52892217-91ec-4e5d-a9f4-1b6cc0a8000a',
'username' => 'burzum',
'slug' => '',
// ...
要从对象中获取某些内容,只需执行以下操作:
echo $user->username;
实际数据位于受保护的属性
实体::$\u properties
中,可由访问 这将在您的控制器中
public function index() {
$this->set('users', $this->paginate($this->Users));
$this->set('_serialize', ['users'])
}
这是你可以付诸行动的
PaginStore logic没有说这是一个bug,我不知道如何使用分页组件获取数据的新方法。好吧,您已经获得了数据。;)通过foreach运行结果并对其进行调试,然后您将看到post-entity对象。您能否举例说明您认为它将如何与分页组件一起工作?也许能帮我弄明白这一点。谢谢。您在视图中放置了什么来显示数据?难道这不再是一种方式吗<代码>echo$user->username$职位->头衔;就像我多次说过的:它是一个对象,不再是数组。实际数据在受保护的属性中,由magic获取访问代码>?
public function index() {
$this->set('users', $this->paginate($this->Users));
$this->set('_serialize', ['users'])
}