active-record icon indicating copy to clipboard operation
active-record copied to clipboard

weird behavior of findAll

Open backend-eigital opened this issue 8 years ago • 6 comments

it is weird why findAll() method should behave different from all other xxxAll()

Table::findAll(
  [
    'id' => 123,
    'check'  => true,
    ['OR', ['userId' => 123], ['userId' => 0]],
  ]
);

What is the expected result?

SELECT * FROM `table` WHERE `id` = 123 AND check = 'true' AND (userId = 0 OR userId = 123)

What do you get instead?

SELECT * FROM `table` WHERE `id` IN (123, 'true', NULL)

Additional info

deleteAll command work as expected and its really weird why thing behave differently and if it want to change the query why it doesn't just throw an exception. like where() which throw exception when we call it with same array

Table::find()->where(
  [
    'id' => 123,
    'check'  => true,
    ['OR', ['userId' => 123], ['userId' => 0]
  ],
])->all();
Q A
Yii version 2.0.13
PHP version 7.1
Operating system Debian 9

backend-eigital avatar Nov 24 '17 08:11 backend-eigital

Thats because findAll() works in the same way as find() - the key-word here is "find" not "all".

rob006 avatar Nov 26 '17 20:11 rob006

so let me ask my question in a new way why they should have different behaviour at all.

still not make sense to me to have things under the concept of finding something (either in findAll() or updateAll() and etc.) but with different behaviour here we only focus on condition part of xxxAll().

backend-eigital avatar Nov 27 '17 04:11 backend-eigital

did you read the documentation of the methods?

cebe avatar Nov 27 '17 12:11 cebe

Yes @cebe, I read it but problem is not in misunderstanding or wrong usage problem is not having a uniform for all/one conditional queries. as developer you know how difficult is to remember things but to make it more developer friendly we can simply have same uniform for all of the All/One queries and prevent this kind of mess.

backend-eigital avatar Nov 28 '17 05:11 backend-eigital

it's like this situation suppose you have a getItemNameById() and getItemDescriptionById() as a developer you expect both function accept same argument and behave similar of course their functionality is different but you generally expect to send same argument to both of them.

backend-eigital avatar Nov 28 '17 05:11 backend-eigital