繁体   English   中英

与CakePHP 3中的“ foreign_key”和“ model”字段进行“关联”关联

[英]'Through' association with 'foreign_key' & 'model' fields in CakePHP 3

这是一个相当标准的事情,我在CakePHP 2中大概做了600次,但是对于我自己的一生,我无法在CakePHP 3中使用它。

我有VideosPhotosArticles 我也有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类,并且尝试了一个伪类(例如上面的“分类”)。

我必须假设这是相当标准的,但是在书中找不到一个示例,而且我似乎无法使其正常工作。


编辑:

我也尝试过这个:

//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”

由于视频和类别不是1-1 o n-1(hasOne或belongsTo),因此不可能构建一个可以包含其他表的条件的SQL表达式。 对于这些情况,CakePHP实现了matching()函数。 它的工作方式与contain()类似,但是它所做的是使用INNER连接从外部关联中获取数据:

http://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#filtering-by-associated-data

您还可以在此处查看使用它的示例:

http://book.cakephp.org/3.0/en/tutorials-and-examples/bookmarks/intro.html#creating-the-finder-method

我最终使它像这样工作:

class VideosTable extends Table
{
    public function initialize(array $config)
    {
        $this->hasMany('Categorized', [
            'foreignKey' => 'foreign_key',
            'conditions' => [
                'Categorized.model' => $this->alias(),
            ]
        ]);
    }

    public function getTopVideosByCategory($categorySlug)
    {
        return $this->find()
            ->matching(
                'Categorized.Categories', function ($q) use ($categorySlug) {
                    return $q
                        ->where(['Categories.slug' => $categorySlug]);
            })
            ->limit(8)
            ->order([
                'Videos.featured' => 'DESC',
                'Videos.created' => 'DESC'
                ])
            ->all();
    }

我强烈评价了José的答案,因为它使我无法确定答案,但会将此标记为答案,因为我认为它更快速地帮助用户尝试解决此特定问题。

José,如果您想将此(在您认为合适的情况下进行任何调整)添加到答案中,我将标记为答案的答案更改为您的答案。

看来您想要的是:

$this->belongsToMany('Categories', [
    'through' => 'Categorized',
    'conditions' => ['Categorized.model' => $this->alias()]
]);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM