如何在cakephp中使用where子句对连接的多对多表进行分页?
我有两张桌子,它们被另一张桌子连接起来。表格如下: 后稳定:如何在cakephp中使用where子句对连接的多对多表进行分页?,php,cakephp,pagination,cakephp-3.0,Php,Cakephp,Pagination,Cakephp 3.0,我有两张桌子,它们被另一张桌子连接起来。表格如下: 后稳定: $this->belongsToMany('Categories', [ 'through' => 'CategoryPost' ]); $this->belongsToMany('Categories', [ 'through' => 'CategoryPost' ]); $this->belongsTo('Categories', [ 'foreignKey' =>
$this->belongsToMany('Categories', [
'through' => 'CategoryPost'
]);
$this->belongsToMany('Categories', [
'through' => 'CategoryPost'
]);
$this->belongsTo('Categories', [
'foreignKey' => 'category_id'
]);
$this->belongsTo('Posts', [
'foreignKey' => 'post_id'
]);
可分类的:
$this->belongsToMany('Categories', [
'through' => 'CategoryPost'
]);
$this->belongsToMany('Categories', [
'through' => 'CategoryPost'
]);
$this->belongsTo('Categories', [
'foreignKey' => 'category_id'
]);
$this->belongsTo('Posts', [
'foreignKey' => 'post_id'
]);
类别PostTable:
$this->belongsToMany('Categories', [
'through' => 'CategoryPost'
]);
$this->belongsToMany('Categories', [
'through' => 'CategoryPost'
]);
$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
控制器中的类别
操作定义如下:
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中的闭包不会在更高的范围内继承变量
导致错误消息的代码如下所示:
$posts = $this->Posts->find()->matching('Categories', function ($q) {
return $q->where(['Categories.safe_name' => $safe_name]);
});
因为在闭包中,没有定义变量$save\u name
。要修复该错误,请使用
tnx修复了很多错误,但弹出了另一个错误:未定义索引:pageCount[CORE\src\View\Helper\PaginatorHelper.php,第634行
你没有调用paginate,所以这并不奇怪。哦,我的错误,我会更新帖子,谢谢你的帮助!我如何检查类别及其在视图中的值?我这样做了,但出错了:如果($post->has('category'){foreach($post->category as$category){echo$category->name;}
这是第三个问题;)-请小心-如果你有新的疑问,这与你最初问的问题没有直接关系:问一个新问题。