【问题标题】:How to get data based on 2 column condition in CakePHP如何在 CakePHP 中根据 2 列条件获取数据
【发布时间】:2020-12-09 06:55:40
【问题描述】:

我正在尝试在 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
        ]
    ]);

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

【问题讨论】:

    标签: cakephp conditional-statements cakephp-3.0 query-builder


    【解决方案1】:

    您所写的查询将找到所有规定条件都为真的任何内容,这显然不是您想要的。我想这可能就是您要找的东西?

    $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,
                ],
            ],
        ]
    ]);
    

    【讨论】:

      猜你喜欢
      • 2014-02-18
      • 2020-08-15
      • 2021-11-29
      • 1970-01-01
      • 1970-01-01
      • 2014-12-17
      • 1970-01-01
      • 2022-07-18
      • 2021-11-15
      相关资源
      最近更新 更多