yii2
yii2 copied to clipboard
hasOne or hasMany return null when $link with two bigint element
The model uses the hasOne method. If the link parameter is an array with two keys and the key is bigint, an error will occur. The reason is that the generated SQL will convert bigint into a string in PHP 7.4. The SQL is as follows:
#sql1
SELECT * From TABLE WHERE (` id1 `, ` id2 `) IN ('bigint ',' bigint ')
At this time, mysql8 cannot find data. At the end of the filterByModels method in the file db/ActiveRelationTrait.php, the andWhere method is called, as follows:
$this ->and Where (['in ', $attributes, $values]);
If the SQL generated by the andWhere method is of the following type, then there is no problem.
#sql2
SELECT * FROM TABLE WHERE (`id1` IN ('bigint1') AND `id2`IN (''bigint2')
If using php8, querying bigint will return an integer, and using sql1 is not a problem,
If using PHP 8 or below, it is also possible to configure data connections when connecting to the database as follows:
'db' => [
.....,
'attributes' => [
\PDO::ATTR_EMULATE_PREPARES => false,
]
]
But the bigint returned by JSON data will exceed the maximum number of JS, so I suggest that andWhere generate sql2. Is this suggestion feasible
Additional info
Q | A |
---|---|
Yii version | 2.0.49.2 |
PHP version | PHP7.4 |
MYSQL version | 8.0.35 |
Operating system | centos7.9 |