Closed idevelop4you closed 6 years ago
Thank you for contributing to this issue. As it has been 90 days since the last activity, we are automatically closing the issue. This is often because the request was already solved in some way and it just wasn't updated or it's no longer applicable. If that's not the case, please feel free to either reopen this issue or open a new one. We will be more than happy to look at it again! You can read more here: https://blog.phalconphp.com/post/github-closing-old-issues
This is still a problem, @sergeyklay. As a workaround I created an unloadRelationship
function in my model:
public function unloadRelationship($alias) {
unset($this->$alias, $this->_related[$alias]);
}
It can be called easily:
$model->unloadRelationship('alias');
I found the solution here, but it feels really hacky: https://forum.phalcon.io/discussion/15407/problem-updating-model-property-when-accesing-to-related-model-v
@niden
This issue still persists, I'm not able to set NULL to a prop which related to another model
$this->hasOne('business_host_id', BusinessHost::class, 'id',
[
'alias' => 'BusinessHost',
]
);
......
$business = Business::findFirstById(1);
$businessHost = $business->getBusinessHost();
$business->setBusinessHostId(NULL);
$business->update();
This code triggers a create on the related model, which is an unexpected behavior.
Expected and Actual Behavior
Having this schema and sample data for a user with related user image (not mandatory):
And this models:
I want to remove a user image
Trying setting the Image property to null:
It does not set the user image_id field to null and is giving no errors.
I then modified the setter so that if the specified model is null, it will also set the foreign key field to null:
This works correctly with the code sample above but if the user related image was accesses at least once, it stop working:
No errors but the image_id still contains the image id value and is not set to null.
Looking at the Model source code it seems that, when accessing the user image related record, it is cached in the *_related array and it is never removed so that when saving the record it save also the cached relation.
The Model setter never the clear _related array:
As a workaround I overridden the __set method in my base model in the following way:
Now I can simply call the setter with a null value and it works correctly:
Details