【发布时间】:2015-11-10 09:59:34
【问题描述】:
PostsTable:
$this->belongsToMany('Categories', [
'through' => 'CategoryPost'
]);
类别表:
$this->belongsToMany('Categories', [
'through' => 'CategoryPost'
]);
CategoryPostTable:
$this->belongsTo('Categories', [
'foreignKey' => 'category_id'
]);
$this->belongsTo('Posts', [
'foreignKey' => 'post_id'
]);
我想显示特定类别的帖子。例如“设计”类别中的帖子。
路由定义为:
$routes->connect('/blog/archive/:safe_name', ['controller' => 'Posts', 'action' => 'category'], ['pass' => ['safe_name']]);
Posts 控制器中的category 动作定义如下:
class PostsController extends AppController
{
...
public function category($safe_name = null)
{
$this->paginate = [
'contain' => ['Photos', 'Categories']
];
$posts = $this->Posts->find()->matching('Categories', function ($q) {
return $q->where(['Categories.safe_name' => $safe_name]);
});
$this->set('posts', $this->paginate($posts));
$this->set('_serialize', ['posts']);
}
...
}
但我得到的是:
Undefined variable: safe_name [APP/Controller\PostsController.php, line 188
谁能帮我解决这个问题!我该怎么做? 抱歉英语不好。
顺便说一句,我的 cakephp 版本是 3.0。
【问题讨论】:
标签: php cakephp pagination cakephp-3.0