cphalcon: [BUG]: Saving related entity in Phalcon 5
Discussed in https://github.com/phalcon/cphalcon/discussions/16203
<div type='discussions-op-text'>Originally posted by slechtic November 11, 2022 Hi,
I’m trying to migrate from Phalcon v3 to v5 but I have a problem with updating related entity.
I have an entity Document and this entity has related entity DocumentHistory.
On Document entity there is hasMany relationship defined:
$this->hasMany('id', DocumentHistory::class, 'idDocument', ['alias' => 'documentHistories']);
DocumentHistory entity has belongsTo relationship:
$this->belongsTo('idDocument', Document::class, 'id',['alias' => 'document']);
when I create new DocumentHistory entity and assign existing Document entity, change some values on this entity (Document) and save DocumentHistory entity, only DocumentHistory is saved (created). Document entity is not updated but foreign key is set on DocumentHistory entity correctly. In case when both entities exist it works correctly.
$documentHistory = new DocumentHistory();
$document = Document::findFirst($id);
$document->setNumber($number);
$documentHistory->setDocument($document);
$documentHistory->save();
On DocumentHistory there is a setter:
public function setDocument(Document $document) {
$this->document = $document;
}
This works for Phalcon v3. This will not be supported in v5 or it is a bug? Thank you.
My environent:
PHP 8.1. Phalcon 5.1.0. Postgresql 11</div>
About this issue
- Original URL
- State: open
- Created 2 years ago
- Comments: 19 (8 by maintainers)
I’ll investigate.
@maxgalbu there are two problems, 1 - the getRelated fetchs always a new record so everytime you do $model->pricingConfig, you effectively call a new model.
2 - lack of a First Level Cache Hopefully I can add it to phalcon5.
Anyway a way to solve this:
The $model->pricingConfig = $pricingConfig; could be $model->pricingConfig = $model->pricingConfig;
This would put the pricingConfig in the dirtyRelated array, that is the first one to be fetched. And it’s the best way to mark a dirtyRelation, so it’s probably going to be a default behaviour.
Hope this helps.
Hi @Jeckerson,
I have encountered the same issue as the OP. Could you give us an information whether it is a bug or not? I need to decide if I need to migrate my project to another FW.