Answer the question
In order to leave comments, you need to log in
How to change the passed object in yii2?
Hello! Maybe a stupid question, but I can't figure it out.
There is a MethodsController controller, it inherits from Controller
. It has a function (public function getUser()) that checks user data, and if successful, selects an object from the database and passes it to return.
That is, roughly, $user = ExtUser::find()->all(); return $user;
There is a SiteController controller that extends MethodsController and calls this very function:
$user = $this->getUser();
As a result, everything seems to be fine. If you peep (var_dump($user)) then the user comes. I can even delete it $user->delete();
But I can not take and change the fields in it. $user->status = 2; $user->save(); - does not save.
Why?
I found the object, sent it, I received it in the SiteController controller, I see it, and I can even delete it. But its fields do not change ..
object(app\models\ExtUser)#88 (11) {
["status"]=>
NULL
["_attributes":"yii\db\BaseActiveRecord":private]=>
array(6) {
["id"]=>
int(3)
["phone"]=>
int(2147483647)
["token"]=>
string(32) "2c2aa1814597cb78967e54c82b2c0b77"
["status"]=>
int(0)
["type_id"]=>
NULL
["created"]=>
string(19) "2021-02-05 08:02:00"
}
["_oldAttributes":"yii\db\BaseActiveRecord":private]=>
array(6) {
["id"]=>
int(3)
["phone"]=>
int(2147483647)
["token"]=>
string(32) "2c2aa1814597cb78967e54c82b2c0b77"
["status"]=>
int(0)
["type_id"]=>
NULL
["created"]=>
string(19) "2021-02-05 08:02:00"
}
["_related":"yii\db\BaseActiveRecord":private]=>
array(0) {
}
["_relationsDependencies":"yii\db\BaseActiveRecord":private]=>
array(0) {
}
["_errors":"yii\base\Model":private]=>
NULL
["_validators":"yii\base\Model":private]=>
NULL
["_scenario":"yii\base\Model":private]=>
string(7) "default"
["_events":"yii\base\Component":private]=>
array(0) {
}
["_eventWildcards":"yii\base\Component":private]=>
array(0) {
}
["_behaviors":"yii\base\Component":private]=>
array(0) {
}
}
Answer the question
In order to leave comments, you need to log in
Does $user->save();
it return true to you? Maybe the validation fails? After saving, look at the $user dump.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question