Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Model handling of Entity $primaryKey casting #8282

Merged
merged 7 commits into from
Dec 6, 2023

Conversation

kenjis
Copy link
Member

@kenjis kenjis commented Dec 1, 2023

Description
Fixes #7797
Supersedes #8271

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@kenjis kenjis marked this pull request as draft December 1, 2023 08:48
@kenjis kenjis force-pushed the fix-model-pk-with-cast branch from 6f7c53d to 4ed4743 Compare December 1, 2023 08:52
@kenjis kenjis changed the title fix: Model handles cast $primaryKey value when using Entity fix: Model handling of Entity $primaryKey casting Dec 1, 2023
@kenjis kenjis force-pushed the fix-model-pk-with-cast branch from 4ed4743 to 2ee4979 Compare December 1, 2023 09:55
@kenjis kenjis added the bug Verified issues on the current code behavior or pull requests that will fix them label Dec 1, 2023
@kenjis kenjis force-pushed the fix-model-pk-with-cast branch from f89e7dc to 87a706c Compare December 1, 2023 22:34
@kenjis
Copy link
Member Author

kenjis commented Dec 2, 2023

batchUpdate() does not work because the following SQL cannot update the columns:

UPDATE `db_uuid`
INNER JOIN (
SELECT '<binary_uuid_value_1>' `id`, 'update1' `value` UNION ALL
SELECT '<binary_uuid_value_2>' `id`, 'update2' `value`
) `_u`
ON `db_uuid`.`id` = `_u`.`id`
SET
`db_uuid`.`value` = `_u`.`value`

The following patch makes the test pass:

--- a/system/Database/MySQLi/Builder.php
+++ b/system/Database/MySQLi/Builder.php
@@ -131,7 +131,7 @@ class Builder extends BaseBuilder
                 " UNION ALL\n",
                 array_map(
                     static fn ($value) => 'SELECT ' . implode(', ', array_map(
-                        static fn ($key, $index) => $index . ' ' . $key,
+                        static fn ($key, $index) => (is_string($index) ? '_binary' . $index : $index) . ' ' . $key,
                         $keys,
                         $value
                     )),

This makes the following SQL:

UPDATE `db_uuid`
INNER JOIN (
SELECT _binary'<binary_uuid_value_1>' `id`, _binary'update1' `value` UNION ALL
SELECT _binary'<binary_uuid_value_2>' `id`, _binary'update2' `value`
) `_u`
ON `db_uuid`.`id` = `_u`.`id`
SET
`db_uuid`.`value` = `_u`.`value`

See https://dev.mysql.com/doc/refman/8.0/en/string-literals.html

This is the same data type issue in batchUpdate() as the following case in PostgreSQL:
https://forum.codeigniter.com/showthread.php?tid=88871

@kenjis kenjis added the database Issues or pull requests that affect the database layer label Dec 2, 2023
system/Model.php Show resolved Hide resolved
tests/_support/Entity/Cast/CastUUID.php Outdated Show resolved Hide resolved
@kenjis
Copy link
Member Author

kenjis commented Dec 4, 2023

@codeigniter4/database-team Review, please.

@kenjis kenjis merged commit fc2bff7 into codeigniter4:develop Dec 6, 2023
61 checks passed
@kenjis kenjis deleted the fix-model-pk-with-cast branch December 6, 2023 23:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them database Issues or pull requests that affect the database layer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: Model inserts the casted $primaryKey value when using an entity and casts.
2 participants