In CakePHP 2 when you write something like that:
$Model->find('all', ['conditions' => ['field' => 1]]);
You will get query which looks like this:
SELECT * FROM model WHERE model.field = 1;
Or when you write something like that:
$Model->find('all', ['conditions' => ['field' => [1,2,3,5]]]);
You will get query which like this:
SELECT * FROM model WHERE model.field IN (1,2,3,5);
What I mean, is that the 'IN' statement is generated automatically depending on the type of argument.
On the other hand in CakePHP 3 when you write
$Table->find()->where(['field' => [1,2,3]])->all();
you will receive an error, e.g.
Cannot convert value to integer
because it generates statement like this:
SELECT * FROM table WHERE table.field = Array();
Is this a bug or a feature?
According to the official docs you need to indicate the column data type in CakePHP 3:-
$Table->find()->where(['field' => [1,2,3], ['field' => 'integer[]']])->all();
integer[]
is telling Cake to expect an array of integers.
Alternatively you can include IN
in your condition:-
$Table->find()->where(['field IN' => [1,2,3]])->all();
Both cases will cast the data to an array if it's not already so that the IN
will work as expected if only an integer value is passed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With