如何让所有用户使用;“订购”;HABTM与CakePHP关系的最新帖子
我在Post和用户模型之间有一个HABTM关系 现在我想接收Post.published订购的所有用户 大概是这样的:如何让所有用户使用;“订购”;HABTM与CakePHP关系的最新帖子,cakephp,cakephp-1.3,Cakephp,Cakephp 1.3,我在Post和用户模型之间有一个HABTM关系 现在我想接收Post.published订购的所有用户 大概是这样的: ... var $paginate = array( 'limit' => 8, 'recursive' => 1, 'fields' => array('id', 'username', 'image') ); function index() { $this->paginate['conditions'] = arra
...
var $paginate = array(
'limit' => 8,
'recursive' => 1,
'fields' => array('id', 'username', 'image')
);
function index() {
$this->paginate['conditions'] = array('User.state'=>true);
$this->paginate['order'] = 'Post.published DESC';
$this->set('authors', $this->paginate());
}
...
我该怎么做?可能吗
在MySQL中:
选择users.id、users.username、users.image、posts.published FROM users内部加入posts\u users ON users.id=posts\u users.user\u id
posts.id=posts\u users.post\u id上的内部连接posts
邮购;已出版的描述代码>
解决方案:
function index() {
$this->paginate['joins'] = array(
array('table' => 'posts_users', 'alias' => 'PostsUser', 'type' => 'inner', 'conditions'=>array('User.id = PostsUser.user_id')),
array('table' => 'posts', 'alias' => 'Post', 'type' => 'inner', 'conditions'=>array('Post.id = PostsUser.post_id'))
);
$this->paginate['fields'] = array('id', 'username', 'image');
$this->paginate['conditions'] = array('User.state'=>true);
$this->paginate['order'] = 'Post.published DESC';
$this->paginate['group'] = 'User.id';
$this->set('authors', $this->paginate());
}
可以在分页选项数组中指定联接,就像在查找选项中一样:
class PostsController extends AppController {
public function by_tag ( $tag ) {
/**
* This will fetch Posts tagged $tag (say, 'PHP')
*/
$this->paginate['Post'] = array(
'limit' => 10,
'contain' => '',
'conditions' => array(
'Post.published' => 1
),
'fields' => array('Post.*', 'Tag.*'),
'joins' => array(
array(
'table' => 'posts_tags',
'type' => 'INNER',
'alias' => 'PostTag',
'conditions' => array(
'Post.id = PostTag.post_id'
)
),
array(
'table' => 'tags',
'alias' => 'Tag',
'type' => 'INNER',
'conditions' => array(
"PostTag.tag_id = Tag.id AND Tag.name = '$tag'"
)
)
)
);
$data = $this->paginate('Post');
$this->set(compact('data'));
}
}
是的,它应该能工作。你的结果是什么?什么有效,什么无效?您的实际代码是什么?MySQL命令工作正常。现在我需要一个蛋糕的方式。。。这是我的问题。如何使用Cake分页(PaginatorHelper)实现此命令?