associations - CakePHP 3 查找没有关联记录的记录 (hasMany)

标签 associations cakephp-3.0 has-many

我有 Products hasMany Tasks

在任务表中查找所有没有关联记录的产品的最佳方法是什么?

我试过了:

$query->matching('Tasks', function ($q) {
return $q->where(['Tasks.product_id' => NULL});

但这似乎不起作用。

最佳答案

正如格雷格·施密特所写:notMatching是解决方案:

$query = $articlesTable->find()->notMatching('Tags');

$query = $articlesTable
->find()
->notMatching('Tags', function ($q) {
    return $q->where(['Tags.name' => 'boring']);
});

关于associations - CakePHP 3 查找没有关联记录的记录 (hasMany),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36737768/

相关文章:

MySQL:从连接表中获取每个最新条目

ruby-on-rails - 如何过滤 ActiveRecord 模型的 association_ids?

mysql - CakePHP 3.x - 关联错误 - 无法将表链接在一起

使用 CMD 关联文件

cakephp-3.0 - 我在 cakephp 3 中将自定义异常存储在哪里?

node.js - 使用 npm 和 CakePHP 3 - 文件位置

ruby - Rails has_many 关联导致 "no such column"错误

php - 如何在 Laravel 中隐藏空类别

ruby-on-rails - 如何计算具有一个或多个关联对象的记录数?

ruby-on-rails - 确保 has_many :through association is unique on creation