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

passing $primaryKey to entity toRawArray() + save() improvements #1772

Closed
wants to merge 14 commits into from

Conversation

nowackipawel
Copy link
Contributor

@nowackipawel nowackipawel commented Feb 28, 2019

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

- 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: 
```
@nowackipawel
Copy link
Contributor Author

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":"[343]","tes_created_at":null,"tes_updated_at":null}
YES UPDATE `t_test` SET `tes_json` = '[343]', `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":"ABC1551430367.506","tes_json":"[80]","tes_created_at":null,"tes_updated_at":null}
YES UPDATE `t_test` SET `tes_txt` = 'ABC1551430367.506', `tes_json` = '[80]', `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":"ABC1551430367.527","tes_json":null,"tes_created_at":null,"tes_updated_at":null}
YES UPDATE `t_test` SET `tes_id` = 144, `tes_txt` = 'ABC1551430367.527'
WHERE `t_test`.`tes_id` = '1'
VALIDATION ERRORS: 
INSERT_TEST tes_id + tes_txt
{"tes_id":53,"tes_txt":"1110.54658200 1551430367","tes_json":null,"tes_created_at":null,"tes_updated_at":null}
53 INSERT INTO `t_test` (`tes_id`, `tes_txt`) VALUES (53, '1110.54658200 1551430367')
VALIDATION ERRORS: 
INSERT_TEST tes_id + tes_txt
{"tes_id":141,"tes_txt":null,"tes_json":null,"tes_created_at":null,"tes_updated_at":null}
141 INSERT INTO `t_test` (`tes_id`) VALUES (141)
VALIDATION ERRORS: 

@nowackipawel
Copy link
Contributor Author

nowackipawel commented Mar 1, 2019

@lonnieezell , @jim-parry :
Could you please tell my why my PRs breaking the tests for sqlite?

  • testSelectAndEntitiesSaveOnlyChangedValues ErrorException: SQLite3::exec(): NOT NULL /home/travis/build/codeigniter4/CodeIgniter4/tests/system/Database/Live/ModelTest.php:919
    It looks like this is trying to update database row but it shouldn't

  • testCanCreateAndSaveEntityClasses /home/travis/build/codeigniter4/CodeIgniter4/tests/system/Database/Live/ModelTest.php:643
    It looks like this is trying to insert entity but it shouldn't ... :/

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.

@jim-parry
Copy link
Contributor

The error messages for the SQLite3 tests seem straightforward.
I would guess that the Model changes you made have had side effects.
There is now a conflict with your Entity changes, and there have been some other Model changes -- I will restart the build, and see if other changes have alleviated some of the errors reported here.

@atishhamte
Copy link
Contributor

I guess, this is already fixed by @lonnieezell in #1829. So there is no need to pass primary key to methods

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants