You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
public function update(Realty $realty, array $params) : Realty
{
if (array_key_exists('metadata', $params)) {
$metadata = $realty->getMetadata();
if ($metadata && empty($params['metadata'])) {
$metadata->delete();
// $realty->getMetadata(); // it's working only if we do that ! fetching the data again
} elseif ($metadata) {
(new MetadataManager())->update($metadata, $params['metadata']);
} else {
(new MetadataManager())->create(bla bla bla);
}
unset($params['metadata']);
}
$realty
->assign($params)
->save(); // at saving, the metadata is recreated even if previously deleted, unless I uncomment previous comment
return $realty;
}
I tried to debug with unit tests with multiple ways. Only two are working :
the one commented, by fetching $realty->getMetadata( ) without even assigning it to anything
or after $metadata->delete(), I re-fetch de initial realty : $realty = Realty::findFirst($realty->id);
PS : ->refresh() won't do it, $realty->getMetadata()->delete() won't do it, $realty->Metadata = null; nope, etc.
💡 I get that Phalcon keep relations stored, etc. and that's why at save it might recreate it, even if it's really confusing. So I'm just wondering what is the best way to do that type of operation in your opinion.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi 👋 I'm working with Phalcon 4 and PHP 7.4
I've got two models
A realty (a real estate property)
and the realty optional metadatas
I'm implementing an API : PUT realty/:id
let's say I do :
then in my manager I would do
I tried to debug with unit tests with multiple ways. Only two are working :
$realty->getMetadata( )
without even assigning it to anything$metadata->delete()
, I re-fetch de initial realty :$realty = Realty::findFirst($realty->id);
PS :
->refresh()
won't do it,$realty->getMetadata()->delete()
won't do it,$realty->Metadata = null;
nope, etc.💡 I get that Phalcon keep relations stored, etc. and that's why at save it might recreate it, even if it's really confusing. So I'm just wondering what is the best way to do that type of operation in your opinion.
the unit test, just in case :
thank you for your advice 👍
Beta Was this translation helpful? Give feedback.
All reactions