简体   繁体   English

如何在CakePHP 3.x的MySQL IN子句中使用多个列?

[英]How to use more than one column inside MySQL IN clause in CakePHP 3.x?

I am trying to execute following SQL through CakePHP 3.x as mentioned below: 我正在尝试通过CakePHP 3.x执行以下SQL,如下所述:

SELECT
 COL1,
 COL2,
 COL3
FROM
 Selections
WHERE
 ROW (COL1, COL2) NOT IN (
  ROW (1, 1),
  ROW (2, 2)
 );

I have stored the values to be filtered as an array 我将要过滤的值存储为数组

$filter = [[1 ,1], [2, 2]];

In CakePHP 3.x controller, I am trying to execute following Query: 在CakePHP 3.x控制器中,我试图执行以下查询:

$conditions['ROW (Selections.COL1, Selections.COL2) NOT IN'] = $filter;
$options = [
    'limit'      => 1,
    'conditions' => $conditions,
    'order'      => ['Selections.created' => 'asc']
];
debug($this->Selections->find('all', $options));
$selections = $this->Selections->find('all', $options);

This execution results in failure due to following CakePHP 3.x error: 由于出现以下CakePHP 3.x错误,此执行导致失败:

Cannot convert value to string
InvalidArgumentException

On debugging the Object, I found that it does not have the correct parameter type (marked in bold below): 在调试对象时,我发现它没有正确的参数类型(下面以粗体标记):

'params' => [
    ':c0' => [
        'value' => [
            (int) 0 => [
                (int) 0 => (int) 1,
                (int) 1 => (int) 1
            ],
            (int) 1 => [
                (int) 0 => (int) 2,
                (int) 1 => (int) 2
            ]
        ],
        'type' => **null**,
        'placeholder' => 'c2'
    ]
],

As per my understanding the type should be 'integer[][]' in order to allow proper conversion of double dimensional array to string. 根据我的理解,类型应为'integer [] []',以允许将二维数组正确转换为字符串。 However, the type being null is probably interfering with proper conversion. 但是,为null的类型可能会干扰正确的转换。

As an alternative, I made an attempt to implode the $filter array thereby converting it into proper string with correct set of brackets and delimiters like this: 另外,我尝试插入$filter数组,从而将其转换为带有正确的方括号和定界符的正确字符串,如下所示:

$filter[] = 'ROW '.implode(', ', [1, 1]);
$conditions['ROW (Selections.COL1, Selections.COL2) NOT IN'] = '('.implode(', ', $filter).')';

This correctly converted the array into string for me but CakePHP goes a step ahead and encapsulates the generated string within quotes. 这对我来说正确地将数组转换为字符串,但是CakePHP向前迈了一步,并将生成的字符串封装在引号中。 This results in following query which throws error during execution on the MySQL db server. 这将导致以下查询,该查询在MySQL数据库服务器上执行期间会引发错误。

SELECT
 COL1,
 COL2,
 COL3
FROM
 Selections
WHERE
 ROW (COL1, COL2) NOT IN '(
  ROW (1, 1),
  ROW (2, 2)
 )';

Please note the extra quote after NOT IN and before bracket. 请注意NOT IN和方括号之前的额外报价。

I am looking for one of the following solutions: 我正在寻找以下解决方案之一:

  1. Is there a way to specify the correct parameter type that would help CakePHP with proper conversion to string of the double dimensional array ? 有没有一种方法可以指定正确的参数类型,以帮助CakePHP正确转换为二维数组的字符串?
  2. If I attempt string conversion myself, as mentioned above, how can I avoid automatic placement of quotes ? 如果如上所述我自己尝试进行字符串转换,如何避免自动放置引号?

I would also appreciate additional information on why CakePHP 3.x is assigning parameter type as NULL in above situation and ways in which this can be avoided. 我还要感谢有关在以上情况下CakePHP 3.x为什么将参数类型分配为NULL以及避免这种情况的其他信息。

If there are alternate methods to handle this case in CakePHP then an attempt to explain it won't go without my sincere appreciation. 如果在CakePHP中有其他方法可以处理这种情况,那么在没有我真诚的感谢的情况下,尝试对其进行解释是不可能的。

Using the second approach of generating string equivalent myself, it is extremely easy to solve this problem: 使用我自己生成字符串等效项的第二种方法,解决此问题非常容易:

$filter[] = 'ROW '.implode(', ', [1, 1]);
$conditions['ROW (Selections.COL1, Selections.COL2) NOT IN'] = '('.implode(', ', $filter).')';

second line should be replaced with: 第二行应替换为:

$conditions['ROW (Printouts.user_id, Printouts.receiver) NOT IN ('.implode(',',$dispatched).')'];

Damn ! 该死的 ! it took less than a line change to solve a problem description that expanded to 2 pages. 解决问题描述扩展到2页所需的时间少于一行。 Basically, one can avoid the 'key' => 'value' format of specifying the condition and just specify the whole condition as 'value' only. 基本上,可以避免指定条件的'key' => 'value'格式,而只将整个条件指定为'value'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM