【发布时间】:2015-04-15 06:35:25
【问题描述】:
这是一个相当标准的事情,我在 CakePHP 2 中可能已经做了 600 次,但在我的一生中,我无法让它在 CakePHP 3 中工作。
我有Videos、Photos、Articles。我也有Categories。
视频、照片和文章都可以属于一个或多个类别。
当前问题的目标是拉取某个类别下的视频。
所以,我尝试了这个:
// VideosTable
$this->belongsToMany('Categories', [
'joinTable' => 'categorized',
'className' => 'Categorized',
'foreignKey' => 'foreign_key',
'conditions' => [
'Categorized.model' => 'Videos',
]
]);
public function getTopVideosByCategory($categorySlug)
{
return $this->Categories->find('all')
->where(['Categories.slug' => $categorySlug])
->contain([
'Videos' => function ($q) {
return $q
->limit(8)
->contain([
'Tags',
'Categories' // tried with and without this
])
->order([
'Videos.featured' => 'DESC',
'Videos.created' => 'DESC'
]);
}
])
->first();
}
错误:SQLSTATE[42S22]:找不到列:1054 未知列 'where 子句'中的'Categorized.model'
我尝试了许多其他方法,包括创建连接表的模型和其他一些方法,但不断出错。我已经尝试了每个选项,并且选项数量有限。我尝试过使用实际的 Table 类,也尝试过伪类(如上面的“Categorized”)。
我不得不假设这是相当标准的,但在书中找不到示例,而且我似乎无法让它发挥作用。
编辑:
我也试过这个:
//VideosTable
public function initialize(array $config)
{
$this->belongsToMany('Categories', [
'through' => 'Categorized',
'conditions' => [
'Categorized.model' => $this->alias(),
]
]);
}
public function getTopVideosByCategory($categorySlug)
{
return $this->find('all')
->matching('Categories', function ($q) use ($categorySlug) {
return $q
->where(['Categories.slug' => $categorySlug]);
})
->contain([
'Tags',
'Categories'
])
->limit(8)
->order([
'Videos.featured' => 'DESC',
'Videos.created' => 'DESC'
])
->first();
}
但是得到这个错误:
错误:SQLSTATE[42S22]:找不到列:1054 未知列 'on 子句'中的'Categorized.model'
【问题讨论】:
-
Categorized 的表架构是什么样的?
-
分类只是
id(CHAR 36),foreign_key(CHAR 36),model(VARCHAR 60),category_id(CHAR 36)
标签: php mysql cakephp associations cakephp-3.0