0

我有几个动作都正确分页,但我无法弄清楚这个“加入”查询发生了什么。它显示了我表中的所有帖子,而不是像我指定的那样一次显示 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

4 回答 4

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 } ?>    
于 2012-08-07T06:03:44.620 回答
0

以下是施展魔法的 3 个步骤。

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

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

2)在第一个控制器中,添加第二个模型用法如下:

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

3) 最后打印 $this->paginate() 你会得到想要的数组。

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

于 2014-01-28T11:26:24.710 回答
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'));
}
于 2012-11-27T11:01:04.630 回答
0

读这个:-

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

分页不喜欢加入

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

<?php
class PostsController extends AppController {

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

Post 和 Author 键的值可以包含模型/键少 $paginate 数组可以包含的所有属性。

于 2012-08-07T03:44:09.240 回答