Model data not updated on second save() #15625
Replies: 4 comments 6 replies
-
Hello, could you also provide example your setters: |
Beta Was this translation helpful? Give feedback.
-
I found what causes the problem. However, I still did not understand why it happens and how to solve it.
$this->belongsTo( 'RequestStatusId', RequestStatus::class, 'Id', [
'alias' => 'RequestStatus',
'reusable' => false,
] ); Let's see the code in the first message of the thread (#15625 (comment)). That code is 100% working, except for the first method invocation: /* Just returns an instance of RequestStatus model (existing record, of course) */
$newStatus = self::calculateNewStatus( $request ); Actually, return StatusSet::calculateStatus( $request->getRequestStatus()->getNewStatusCode() ); The point is that doing this: $request->getRequestStatus(); just before doing this (see the code in the first message): $request->setRequestStatusId( $newStatus->getId() );
$request->save(); Causes the problem that In the code above I can reproduce the problem (1) or I can correctly save the record (2): (1): var_dump( $request->getRequestStatusId() ); // returns "111"
/* Make the ORM perform the foreign key query */
$request->getRequestStatus();
/* This method saves $request (another field, not "RequestStatusId"): successfull */
$this->evaluateApply( $request );
$request->setRequestStatusId( 222 );
$request->save();
var_dump( $request->getRequestStatusId() ); // returns "111" (2): var_dump( $request->getRequestStatusId() ); // returns "111"
/* DON'T perform the foreign key query */
/*** $request->getRequestStatus; ***/
/* This method saves $request (another field, not "RequestStatusId"): successfull */
$this->evaluateApply( $request );
$request->setRequestStatusId( 222 );
$request->save();
var_dump( $request->getRequestStatusId() ); // returns "222" |
Beta Was this translation helpful? Give feedback.
-
I created an issue for this: #15649 |
Beta Was this translation helpful? Give feedback.
-
To reproduce |
Beta Was this translation helpful? Give feedback.
-
We are upgrading our application from Phalcon 3.4.5 to Phalcon 4.1.2 and from PHP 7.3 to PHP 7.4.
Everything work fine except for one serious problem.
We have a model "
Request
" which usesprotected
properties andpublic
getters and setters and with Exception onsave()
failure. There is a Controller which updates the model twice on an already existing record, using another method of the Controller class inside a transaction.Code:
As you can see the
$request
record is updated (save
d) twice: once inside another method which accepts the model instance as parameter and then inside the main method.This code has always worked great.
PROBLEM
After the second
$request->save()
the instance of$request
contains:UserAssignedId
(first save)RequestStatusId
(second save)Dirty state is alway "TRANSIENT" on all stages.
Please note that we tried
var_dump()
the model data before and after every statement. TheRequestStatusId
value actually changes. It is correct until the beginning of thesave()
method (we extended thesave()
method just tovar_dump()
the value of the field before callingreturn parent::save()
).The value is the NEW one until before
return parent::save()
. Then the value changes back to the old one already inside the first model events:beforeValidation()
,afterValidationOnUpdate()
,beforeSave()
,afterSave()
.The only way to make the model perform the correct second update is to do this:
I mean, we must
refresh()
the model before performing the subsequent assignment. This is obviously unacceptable because we have plenty of situations of this kind in our application. And it is unacceptable because we're working with objects...Please, can you help us?
Thank you
Beta Was this translation helpful? Give feedback.
All reactions