CakePHP:"IN"-运算符对单值和多值的工作方式不同

CakePHP: "IN"-operator works differently for single value and multiple values

我正在使用此查询来获取匹配结果:

$result = $this->Customer->CustomersC->find('list', array(
  'conditions' => array('CustomersC.company_id IN' => $companyIds),
  'fields' => array('CustomersC.customer_id'),
));

如果 $companyIds 有多个值,查询就完美了。这是日志中打印的查询:

SELECT CustomersC.customer_id FROM project.customers_companies AS CustomersC WHERE CustomersC.company_id IN (7, 8, 9, 11, 15)

但是,如果 $companyIds 只有一个值,它会抛出 SQL 异常。这是查询:

SELECT CustomersC.customer_id FROM project.customers_companies AS CustomersC WHERE CustomersC.company_id IN = ('7')

请注意在IN.

之后添加的不必要的=(等于运算符)

有人可以指出我处理这种情况的正确方向吗?

您无需在条件中指定 IN。您只需要执行以下操作:-

$result = $this->Customer->CustomersC->find('list', array(
    'conditions' => array('CustomersC.company_id' => $companyIds),
    'fields' => array('CustomersC.customer_id'),
));

CakePHP 将根据条件值确定您是指 = 还是 IN。如果你传递一个数组,它会将条件视为 IN。在您损坏的情况下,它使用 = 但您的条件键中有 IN,导致 IN = 错误。