如何在CakePHP中基于2列条件获取数据

pbossiut  于 2022-11-12  发布在  PHP
关注(0)|答案(1)|浏览(132)

我试图在CakePHP 3.6中获取一些数据。条件是基于同一个表中的2列。第一列是status,第二列是is_done_by_user。所以我想在状态是12时获取数据。同样,如果状态是3。但对于状态3,需要检查列is_done_by_user是否具有值1。因此,最后一件事是。我希望获得具有status12的所有数据。所有状态为3的数据,其中is_done_by_user1。我已经编写了查询,但它没有按照我想要的方式工作。以下是我到目前为止尝试的查询。

$query = $this->find('all',[
        'conditions' => [
            'Appointments.is_active' => 1,
            'Appointments.status IN' => (1,2,3),
            'Appointments.is_done_by_user' => 1
        ]
    ]);

也许我离实际的查询很远。

fdx2calv

fdx2calv1#

您的查询将找到所有陈述的条件都为真的任何内容,这显然不是您想要的。我认为这可能是您要寻找的内容?

$query = $this->find('all',[
    'conditions' => [
        // This must always be true
        'Appointments.is_active' => 1,
        // One of the conditions in this list must be true
        'OR' => [
            // Either the status is 1 or 2
            'Appointments.status IN' => (1,2),
            // Or the status is 3 AND the user is 1
            [
                'Appointments.status' => 3,
                'Appointments.is_done_by_user' => 1,
            ],
        ],
    ]
]);

相关问题