-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
[BUG]: When updating including a relation, the relation destination is not updated #15148
Comments
I couldn't find anything in the docs (https://docs.phalcon.io/4.0/en/db-models-relationships) about this use case. I've always used it like your second example for updates: $user = User::find(1);
$userCard = $user->userCard;
$userCard->token = $token;
$user->userCard = $userCard;
$user->save(); // update! @phalcon/core-team, what's your opinion? |
I have the same problem: # (Address BelongsTo User)
$address = Address::find(1);
$user = new User();
$user->address = $address;
# 3.4: _preSaveRelatedRecords() triggered, _preSave() triggered
# 4.0: _preSaveRelatedRecords() triggered, _preSave() triggered
$user->save();
$user->address->city = 'Paris';
# 3.4: _preSaveRelatedRecords() triggered, _preSave() triggered (saved)
# 4.0: _preSaveRelatedRecords() NOT triggered, _preSave() triggered (NOT saved)
$user->save(); However, reinserting the related record does not work for me either: # ...
$address = $user->address;
$address->city = 'Paris';
$user->address = $address;
# 3.4: _preSaveRelatedRecords() triggered, _preSave() triggered (saved)
# 4.0: _preSaveRelatedRecords() NOT triggered, _preSave() triggered (NOT saved)
$user->save(); Setting I did not find an adequate option in the documentation, I'm just Imagine that it should work using
Any idea? |
We are going to fix and document this before the next release. |
Added new method collectDirtyRelated() Refs: phalcon#15148
Added new method collectDirtyRelated() Refs: #15148
Fixed in #15195 and just released with 4.1 |
Thank you very much for your great work @zsilbi. Your fix works for me, but maybe only partially. With hasOne / hasMany relationships (as in your test), it's perfect. However, if you replace
But maybe it's a problem of understanding on my part... |
@gponcon Seems that in your case
|
Thank you @Jeckerson for this clarification. I asked this question because we have a lot of
With Phalcon 4, In the unit tests, we have a |
Let's reopen this until I add some more tests to verify this. |
You can try with public function mvcModelSaveWithRelatedManyAndBelongsRecordsProperty(DatabaseTester $I)
{
$I->wantToTest('Mvc\Model - save() with related records property (relation many - belongs)');
/** @var \PDO $connection */
$connection = $I->getConnection();
$invoicesMigration = new InvoicesMigration($connection);
$invoicesMigration->clear();
$invoicesMigration->insert(77, 1, 0, uniqid('inv-', true));
$customersMigration = new CustomersMigration($connection);
$customersMigration->clear();
$customersMigration->insert(1, 1, 'test_firstName_1', 'test_lastName_1');
/**
* @var Invoices $invoice
*/
$invoice = InvoicesKeepSnapshots::findFirst(77);
$I->assertEquals(
1,
$invoice->customer->id
);
$invoice->customer->cst_name_first = 'new_firstName';
$invoice->customer->cst_status_flag = 0;
$I->assertTrue(
$invoice->save()
);
/**
* @var Customers $customer
*/
$customer = Customers::findFirst(1);
$I->assertEquals(
'new_firstName',
$customer->cst_name_first
);
$I->assertEquals(
0,
$customer->cst_status_flag
);
} |
I have not yet verified if this issue has been resolved.
It is necessary to set the above two and verify whether it works. |
For both // ...
public function initialize()
{
$this->keepSnapshots(true);
$this->setSource('co_customers');
$this->hasMany(
cst_id',
InvoicesKeepSnapshots::class,
inv_cst_id',
[
alias' => 'invoices',
reusable' => true,
]
);
}
// ... (but I'm trying to investigate. |
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. |
Hello, $invoice = Invoice::findFirstById(1);
$invoice->user->total_invoices = 10;
$invoice->save(); // doens't work
$invoice->user->save(); // work Invoice Model: $this->belongsTo(
'user_id',
'User',
'id',
[
'alias' => 'user',
'reusable' => true
]
); Setting reusable to false
|
Thank you. $invoice = Invoice::findFirstById(1);
$invoice->user->total_invoices = 10;
$invoice->save(); // doens't work ← this issue
$invoice->user->save(); // work |
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 |
@zsilbi @niden Here is an example: $user = User::findFirstById(1);
foreach ($user->getInvoices() as $item) {
// Just check something here without any changes
}
foreach ($user->getSomeAnotherRelatedModels() as $item) {
// Just check something here without any changes
}
foreach ($user->getSomeThirdTypeRelatedModels() as $item) {
// Just check something here without any changes
}
$user->someProp = 'some value';
$user->save(); 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 Cascade updates like $model = SomeModel::findFirst();
$model2 = $model->getSomeRelatedModel(); // No changes was made to $model2
$items = $model2->getSomeModels(); // No changes was made to $items
$model3 = $model2->getSomeRelatedModel(); // No changes was made to $model3
$items2 = $model3->getSomeModels(); // No changes was made to $items2
$model->someValue = 'value';
$model->save(); // Updates all of the models above slow down our application, we need an approach to turn off this behaviour. |
@kirill-voronov We're having the exact same issue in our application after updating to Phalcon 5. Any fix found yet? :) |
Describe the bug
The following updates will not be updated.
You can reinsert the object changed in this way,
It works if you run update directly at the relation destination.
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
Additional context
Add any other context about the problem here.
The text was updated successfully, but these errors were encountered: