Cakephp 2.8.2 中 IN 子句的奇怪行为



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

$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 自动将 IN(( 转换为 IN = (( 以获得单个 id。

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它(。

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

最新更新