gpt4 book ai didi

php - 使用 cakephp 对连接表进行分页

转载 作者:行者123 更新时间:2023-11-29 08:48:53 24 4
gpt4 key购买 nike

我有几个操作都可以正确分页,但我无法弄清楚这个“连接”查询发生了什么。它显示了我表中的所有帖子,而不是像我指定的那样一次只显示 3 个帖子。

 public function index() { 
$this->paginate = array(
'joins' => array(
array(
'table' => 'Users',
'type' => 'inner',
'limit' => 3,
'fields' => array('User.username'),
'conditions' => array('Users.id = Post.user_id')
)
));
$posts = $this->paginate('Post');
$this->set(compact('posts'));
}

在此处编辑

table Users
id | username | password

table Posts
id | body | user_id | created

此函数可以工作,但无法分页。

public function index() { 
$options['joins'] = array(
array('table' => 'Users',
'type' => 'inner',
'fields' => array('User.username'),
'conditions' => array('Users.id = Post.user_id')
)
);
$post = $this->Post->find('all', $options);
$this->set('posts', $post);
}

最佳答案

试试这个:

Your Model class should have relationship like :

Post Model :
public $belongsTo = array('User');

Post Controller :

$this->Post->recursive = 1;
$this->set('posts', $this->paginate());

Index View :
<table >
<thead>
<tr>
<th><?php echo $this->Paginator->sort('id'); ?></th>
<th><?php echo $this->Paginator->sort('body'); ?></th>
<th><?php echo $this->Paginator->sort('User.username'); ?></th>
<tr>
<?php foreach ($posts as $post){ ?>
<tr>
<td><?php echo h($post['Post']['id']); ?>&nbsp;</td>
<td><?php echo h($post['Post']['body']); ?>&nbsp;</td>
<td><?php echo h($post['User']['username']); ?>&nbsp;</td>
</tr>
<?php } ?>

关于php - 使用 cakephp 对连接表进行分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11838843/

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