2012-08-07 47 views
0

我有幾個操作,所有分頁正確,但我無法弄清楚這個「加入」查詢發生了什麼。它顯示我表格中的所有帖子,而不是像我指定的那樣僅顯示3個帖子。與cakephp分頁加入表

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); 
} 
+0

爲了模仿正常的蛋糕行爲,添加'別名'到連接數組是有幫助的。然後您的數據結果將被正確編入索引(例如$ data ['alias'])。 – zmonteca 2013-04-16 20:10:24

回答

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關鍵字的值可能包含模型/關鍵值減$ paginate數組可能的所有屬性。

+0

提供給我以下錯誤「錯誤:SQLSTATE [42000]:語法錯誤或訪問衝突:1066不唯一表/別名:'用戶'」。這是否意味着我的模型有一些問題? – user1104854 2012-08-07 03:52:09

+0

提供您的用戶表的名稱和型號的類名 – 2012-08-07 03:59:32

+0

我看到了你的表名請提供模型類名,也可以使用用戶或用戶模型 – 2012-08-07 04:08:03

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 } ?>  
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')); 
} 
0

以下是3個步驟是做了魔力。

1)在第一種模式,只需添加第一與第二

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

2)在第一控制器的$的hasMany關係,添加第二模型用法如下:

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

3)最後打印$ this-> paginate(),你將得到所需的數組。

NTB: In the $hasMany array of First Model, add as many secondary tables as wanted.