用cakephp分页连接表

时间:2012-08-07 03:27:31

标签: php mysql cakephp

我有几个行为都正确分页,但我无法弄清楚这个“加入”查询发生了什么。它显示了我表格中的所有帖子,而不仅仅是我指定的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);
}

4 个答案:

答案 0 :(得分:2)

试试这个:

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 } ?>    

答案 1 :(得分:0)

阅读本文: -

http://book.cakephp.org/2.0/en/core-libraries/components/pagination.html

Paginate不喜欢加入

除了定义一般分页值之外,您还可以在控制器中定义多组分页默认值,只需在要配置的模型之后命名数组的键:

<?php
class PostsController extends AppController {

    public $paginate = array(
        'Post' => array (...),
        'Author' => array (...)
    );
}

Post和Author键的值可以包含model / key less $ paginate数组可以包含的所有属性。

答案 2 :(得分:0)

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

答案 3 :(得分:0)

以下是完成魔术的3个步骤。

1)在First Model中,只需添加First with Second的$ hasMany关系

public $hasMany = array(
                ‘Second’ => array(
                     ‘className’ => ‘Second’,
                     ‘foreignKey’ => ‘first_id’,
                     ‘dependent’ => false,
                     ‘conditions’ => ”,
                     ‘fields’ => ”,
                     ‘order’ => ”,
                     ‘limit’ => ”,
                     ‘offset’ => ”,
                     ‘exclusive’ => ”,
                     ‘finderQuery’ => ”,
                     ‘counterQuery’ => ”
     )
);

2)在First Controller中,按如下方式添加第二个模型用法:

public $uses = array(‘First’,'Second’);

3)最后打印$ this-&gt; paginate(),您将获得所需的数组。

  

NTB:在First Model的$ hasMany数组中,添加所需数量的辅助表。