Cakephp 2.8.2 中 IN 子句的奇怪行为
Strange behaviour of IN clause in Cakephp 2.8.2
我创建了多个复选框,如下所示。
$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')
有人可以帮忙吗?
提前致谢
只需替换:
$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
)。
来源:
我创建了多个复选框,如下所示。
$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')
有人可以帮忙吗?
提前致谢
只需替换:
$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
)。
来源: