2

哇,CakePHP 真的没有解决这个问题。

经过数小时的搜索,我遇到了下面的解决方案(可能会或可能不会过时),但我在应用 paginatior 'limit' => 10 或其他排序时遇到问题。

有什么我想念的想法吗?

我的模型:

public $hasAndBelongsToMany = array(
    'Post' => array(
        'className'              => 'Post',
        'joinTable'              => 'tags_posts',
        'foreignKey'             => 'tag_id',
        'associationForeignKey'  => 'post_id',  
        'order'                  => array('Post.created DESC'),
        'unique'                 => true
    )
);

在我的控制器中 view()

public function view($id) {
    $this->Tag->bindModel(array('hasOne' => array('TagsPost')), false);     
    $this->set('tag', $this->paginate('Tag', array('TagsPost.tag_id' => $id))); 
}

在我看来,我不得不改变:

foreach ($tag['Post'] as $post)

foreach ($tag[0]['Post'] as $post)
4

2 回答 2

2

您的视图方法应如下所示:

public function view($id) {
      $this->Paginate['limit'] = 10;
      $this->Paginate['conditions'] = array('TagsPost.tag_id' => $id);
      $this->Tag->bindModel(array('hasOne' => array('TagsPost')), false);     
      $this->set('tag', $this->paginate('Tag')); 
}

请询问它是否不适合您。

于 2012-07-23T04:48:21.807 回答
0

用 CakePHP 2.x 测试

public function view ($id)
{
  $this->paginate = array (
                        'limit' => 10,
                        'conditions' => array('TagsPost.tag_id' => $id)
                    );
  $this->Tag->bindModel(array('hasOne' => array('TagsPost')), false);     
  $this->set('tag', $this->paginate('Tag')); 
}
于 2013-02-18T15:33:04.677 回答