从hasMany关联中检索匹配与否的Cakephp3

时间:2018-01-25 14:37:04

标签: cakephp cakephp-3.0

以下情况 - 我的用户可以进行多项测试 - 关系是users hasMany tests。现在,我需要写一个选择什么会检索我所有有status = 1蚂蚁的用户,这些用户没有接受过任何测试,或者他们的测试是在六个月前拍摄的。我写了第一部分是这样的:

$users = $this->Users->find('all')
    ->where(['Users.status' => 1])
    ->matching('Tests', function($q) {
        return $q
            ->where(['Tests.created <' => new \DateTime('-6 months')]);
       });

我有什么想法可以添加第二位吗?

1 个答案:

答案 0 :(得分:3)

你的问题不是很清楚但似乎你根本不想要在过去6个月内至少参加过一次考试的学生

在这种情况下,我认为notMatching应该完成这项工作

$users = $this->Users->find('all')
    ->where(['Users.status' => 1])
    ->notMatching('Tests', function($q) {
        return $q
            ->where(['Tests.created >=' => new \DateTime('-6 months')]);
    });

您的情况与食谱中描述的here非常相似

相关问题