【问题标题】:CakePHP: "IN"-operator works differently for single value and multiple valuesCakePHP:“IN”-操作符对单值和多值的工作方式不同
【发布时间】:2015-11-02 11:07:40
【问题描述】:

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

$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 之后添加的不必要的 =(equalTo 运算符)。

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

【问题讨论】:

    标签: cakephp in-operator


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-09
      • 1970-01-01
      • 2013-09-10
      • 1970-01-01
      • 1970-01-01
      • 2018-02-16
      • 1970-01-01
      • 2021-12-20
      相关资源
      最近更新 更多