-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
passing $primaryKey to entity toRawArray() + save() improvements #1772
Conversation
- passing primaryKey to toRawAarray method - removing ``` // Always grab the primary key otherwise updates will fail. if (! empty($properties) && ! empty($primaryKey) && ! in_array($primaryKey, $properties)) { $properties[$primaryKey] = $data->{$primaryKey}; } ``` IMHO it is better to get primaryKey value from Entity instead of $data parameter; We will see if this will break some tests or not.
better approach :).
Small fix. I could write: `if(empty($data) || (is_array($data) && count($data) === 1 && isset($data[$this->primaryKey]))` instead of `if(empty($data))` two lines bellow, but approach what I've used I think is better. As it was before we are denying simple updates like `UPDATE primaryKey = newVal WHERE primaryKey = oldVal` In chosen scenario inserts like `INSERT INTO x (primaryKey) VALUES (primaryKey)` will work as before. Run, run travis.
conceptual tests. rewert entity toRawArray
save() - concept revert classToArray fragment: ``` // Always grab the primary key otherwise updates will fail. if (! empty($properties) && ! empty($primaryKey) && ! in_array($primaryKey, $properties)) { $properties[$primaryKey] = $data->{$primaryKey}; } ```
conceptual changes ability to do update x primarykey = new where primarykey = old;
hope this will pass sqlite tests ``` UPDATE_TEST getting tes_id primaryKey - 144 -> 1 {"tes_id":"144","tes_txt":null,"tes_json":null,"tes_created_at":null,"tes_updated_at":null} YES UPDATE `t_test` SET `tes_id` = 1 WHERE `t_test`.`tes_id` = '144' VALIDATION ERRORS: UPDATE_TEST getting tes_id, tes_json - CHANGE json_col - X -> random one element long array {"tes_id":"1","tes_txt":null,"tes_json":"[587]","tes_created_at":null,"tes_updated_at":null} YES UPDATE `t_test` SET `tes_json` = '[587]', `tes_id` = 1 WHERE `t_test`.`tes_id` = '1' VALIDATION ERRORS: UPDATE_TEST getting tes_id, tes_json - CHANGE json_col - X -> random one element long array, tes_txt -> random text {"tes_id":"1","tes_txt":"ABC1551430122.152","tes_json":"[115]","tes_created_at":null,"tes_updated_at":null} YES UPDATE `t_test` SET `tes_txt` = 'ABC1551430122.152', `tes_json` = '[115]', `tes_id` = 1 WHERE `t_test`.`tes_id` = '1' VALIDATION ERRORS: UPDATE_TEST getting tes_id primaryKey - 1 -> 144, tes_txt -> random text {"tes_id":144,"tes_txt":"ABC1551430122.1712","tes_json":null,"tes_created_at":null,"tes_updated_at":null} YES UPDATE `t_test` SET `tes_id` = 144, `tes_txt` = 'ABC1551430122.1712' WHERE `t_test`.`tes_id` = '1' VALIDATION ERRORS: INSERT_TEST tes_id + tes_txt {"tes_id":144,"tes_txt":"ABC1551430122.1712","tes_json":null,"tes_created_at":null,"tes_updated_at":null} 92 INSERT INTO `t_test` (`tes_id`, `tes_txt`) VALUES (92, '1110.19009500 1551430122') VALIDATION ERRORS: INSERT_TEST tes_id + tes_txt {"tes_id":144,"tes_txt":"ABC1551430122.1712","tes_json":null,"tes_created_at":null,"tes_updated_at":null} 146 INSERT INTO `t_test` (`tes_id`, `tes_txt`) VALUES (146, '123') VALIDATION ERRORS: ```
tests
|
@lonnieezell , @jim-parry :
I tried to replicate (without PHPUnit) on my local machine and I cannot replicate it :/ I don't want to bother you, but I think this approach to save() method is better as it was before changes. |
The error messages for the SQLite3 tests seem straightforward. |
I guess, this is already fixed by @lonnieezell in #1829. So there is no need to pass primary key to methods |
Fixing: #1770
save() will try to insert() instead of update() in some cases [$data is instance of Entity].
i think this is fixing another issue with inserting entities with non-autoincrement primaryKeys - then save() tried to produce update()
we will see what kind of test this will break :D