Closed procin closed 1 month ago
Hi @procin ,
It might be due to the update of the PHP version and the underlying MySQL driver. Were you by any chance using the "libmysql" driver before and now using the "mysqlnd" driver?
Hi @rhertogh ,
Thanks for your reply. That would be mysqlnd 7.4.33 when it was working well, versus mysqlnd 8.3.11 at this moment. So you would expect pretty simular behaviour.
As the yii2 guide states: Note: While this method saves memory and improves performance, it is closer to the lower DB abstraction layer and you will lose most of the Active Record features. A very important distinction lies in the data type of the column values. When you return data in Active Record instances, column values will be automatically typecast according to the actual column types; on the other hand when you return data in arrays, column values will be strings (since they are the result of PDO without any processing), regardless their actual column types.
This would still be the expected behaviour. Now in my code if an "integer" is matched coming from asArray(), I check === '1' for instance, so very hard to debug which parts of the code will give problems now these values are typecasted.
Are you already working with PHP 8.3? Or are you handling these comparisons in a different way? Thanks!
The note in the docs doesn't promise strings but warns that types aren't cast and are provided as is. Not sure if we should change that.
@yiisoft/reviewers, @yiisoft/yii2 what do you think?
I would not change that to not add additional layer that would have to be maintained unfortunately. Too many factors impacting how this is returned, DB itself, drivers, etc.
Agree.
This would still be the expected behaviour. Now in my code if an "integer" is matched coming from asArray(), I check === '1' for instance, so very hard to debug which parts of the code will give problems now these values are typecasted.
Why not do simple comparison (i.e. x == '1'
) instead of strict comparison (x === '1'
) in that case? I do not see advantage of strict comparison here.
See for more details: https://www.php.net/manual/en/language.operators.comparison.php
Hi @mtangoo,
Thanks for your reply. In the past these values were always strings. I am then used to always using strict comparison if the type is known. But now I doubt between ( (int)x === 1 ) and ( x == 1 ), I think the first could be slightly faster.
@terabytesoftw Yes thanks! This is what I was looking for!
What steps will reproduce the problem?
I'm using PHP 8.3.11 (maybe this also happens with other versions of PHP 8) When using ->asArray()->all() to find the data from a table, I expect integers to be of type string, but now they are already typecasted. Is this due to any settings of PDO? I would like to keep this as string to keep my code the way it is.
What is the expected result?
Before this I was using PHP 7.4, this returned integers as string when using asArray() (as also mentioned in the Yii2 documentation)
What do you get instead?
Integers are already typecasted to type integer.
Additional info