cphalcon: [BUG]: When updating including a relation, the relation destination is not updated
Describe the bug
The following updates will not be updated.
$user = User::find(1);
$user->userCard->token = $token;
$user->save(); // not update(4.0), update(3.4)
You can reinsert the object changed in this way,
$user = User::find(1);
$userCard = $user->userCard;
$userCard->token = $token;
$user->userCard = $userCard;
$user->save(); // update!
It works if you run update directly at the relation destination.
$user = User::find(1);
$user->userCard->token = $token;
$user->userCard->save(); // update!
I think this is because it has not been possible to detect that the data of the relation destination has been dirty. This worked until v3.4.
Details
- Phalcon version: 4.0.6
- PHP Version: 7.4.9
- Operating System: debian(docker)
- Installation type: Compiling from source
- Zephir version (if any):
- Server: Apache
- Other related info (Database, table schema):
Additional context Add any other context about the problem here.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 4
- Comments: 18 (6 by maintainers)
Commits related to this issue
- fix(model): saving changed related records Added new method collectDirtyRelated() Refs: #15148 — committed to zsilbi/cphalcon by zsilbi 4 years ago
- fix(model): saving changed related records Added new method collectDirtyRelated() Refs: #15148 — committed to phalcon/cphalcon by zsilbi 4 years ago
We are going to fix and document this before the next release.
Fixed in https://github.com/phalcon/cphalcon/pull/15195 and just released with 4.1
@zsilbi @niden Hello, we are trying to migrate from 4.0 to 5.0 and got unexpected behaviour because now related records are updating always even if they haven’t changed.
Here is an example:
This will trigger saving user and all related models. If we have 100 related invoices + 100 someAnotherRelatedModels + 100 someThirdTypeRelatedModels, there will be 301 update query sent into DB and 300 of those queries are parasite, because there wasn’t actual changes in related models.
This happens even if
reusableoption is enabled or not because code block here was commented (and all queried relations now always put intothis->related) and hererelatedanddirtyRelatedare join together.Cascade updates like
slow down our application, we need an approach to turn off this behaviour.
I have a similar problem but with an even simpler reproduction.
If I don’t set my toolbox to null in the first place, every test is a success. It seems we can’t instantiate two times that same alias ?
PS : I’m still on 4.0.3, I will test it after I migrate to the latest PS2 : ‘reusable’ => true, didn’t change anything
Hello, After a lot of trial and error dealing with this issue, I realized that it’s working only when you set
reusabletotrue.Invoice Model:
Setting reusable to false
$invoice->user->save();doesn’t work either.I don’t know if I’ve done it right, but if I make these changes it seems to work:
https://github.com/phalcon/cphalcon/pull/15203/files
Note that I am not yet familiar with Zephir and the algorithm behind the MVC models.
At your disposal.
You can try with
CustomersKeepSnapshotsfor example: