【问题标题】:Strange behaviour of IN clause in Cakephp 2.8.2Cakephp 2.8.2 中 IN 子句的奇怪行为
【发布时间】:2017-07-11 11:59:55
【问题描述】:

我创建了多个复选框,如下所示。

 $forms  = $this->wondr->m('WApplication.ApplicantForms')->find('list', array('conditions' => array('facility_id' => $this->wondr->facility['Facility']['id']), 'contain' => array()));

    $event->result['forms'] = $event->subject()->Form->input(
            'forms', array(
                'multiple'   => 'checkbox',
                'options' => $forms,
                'class'  => 'form-control',
                'label'  => __('Forms'),
            )
       );

当我选择多个复选框时它工作正常,但是当我只选择一个复选框时它会抛出错误

   $forms  = $this->wondr->m('WApplication.ApplicantForms')->find('list', array('conditions' => array('facility_id' => $this->wondr->facility['Facility']['id'],
        'ApplicantForms.id IN '=> $event->data['requestQuery']['forms']
    ), 'contain' => array()));

CakePHP 自动将单个 id 的 IN() 转换为 IN = ()。

Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '= ('595b5fab-97d4-4c59-a41a-013fac120009')' at line 1

SQL Query: SELECT `ApplicantForms`.`id`, `ApplicantForms`.`name` FROM `wondr`.`applicant_forms` AS `ApplicantForms` WHERE `facility_id` = '58ecb98f-92cc-4a90-a692-0026ac120005' AND `ApplicantForms`.`id` IN = ('595b5fab-97d4-4c59-a41a-013fac120009')

有人可以帮忙吗?

提前致谢

【问题讨论】:

  • 尝试去掉IN,并确保$event->data['requestQuery']['forms'] 是一个数组。
  • $event->data['requestQuery']['forms'] 是一个数组。

标签: php behavior clause cakephp-2.8


【解决方案1】:

只需替换:

$forms  = $this->wondr->m('WApplication.ApplicantForms')->find('list', array('conditions' => array('facility_id' => $this->wondr->facility['Facility']['id'],
    'ApplicantForms.id IN '=> $event->data['requestQuery']['forms']
), 'contain' => array()));

作者:

$forms  = $this->wondr->m('WApplication.ApplicantForms')->find('list', array('conditions' => array('facility_id' => $this->wondr->facility['Facility']['id'],
    'ApplicantForms.id'=> (array)$event->data['requestQuery']['forms']
), 'contain' => array()));

换句话说,删除IN 并确保$event->data['requestQuery']['forms'] 是一个数组(如果您有疑问,可以先var_dump 它)。

来源:https://stackoverflow.com/a/9445104/978690

【讨论】:

    猜你喜欢
    • 2011-04-02
    • 1970-01-01
    • 2017-12-24
    • 1970-01-01
    • 2017-03-29
    • 2012-05-09
    • 1970-01-01
    • 1970-01-01
    • 2012-08-08
    相关资源
    最近更新 更多