Answer the question
In order to leave comments, you need to log in
Why doesn't $user->save(); work?
The code:
public function editdata()
{
$user = User::findOne($this->username);
if($this->email != null)$user->email = $this->email;
if($this->fio != null)$user->fio = $this->fio;
if($this->apartment != null)$user->apartment = $this->apartment;
if($this->house != null)$user->house = $this->house;
if($this->housing != null)$user->housing = $this->housing;
if($this->street != null)$user->street = $this->street;
if($this->phone != null)$user->phone = $this->phone;
if($this->password != null)$user->setPassword($this->password);
return $user->save();
}
Call to a member function save() on null
Answer the question
In order to leave comments, you need to log in
findOne()
searches by primary key, by id. therefore it does not find it is
necessary like this:
and to avoid such errors, add if
public function editdata()
{
if($user = User::find()->where(['username'=>$this->username])->one()){
if($this->email != null)$user->email = $this->email;
if($this->fio != null)$user->fio = $this->fio;
if($this->apartment != null)$user->apartment = $this->apartment;
if($this->house != null)$user->house = $this->house;
if($this->housing != null)$user->housing = $this->housing;
if($this->street != null)$user->street = $this->street;
if($this->phone != null)$user->phone = $this->phone;
if($this->password != null)$user->setPassword($this->password);
return $user->save();
}
return false;
}
Because User::findOne returns null. See what you have in $this->username and see if there really is such a user. Judging by the code, you receive a changed username from the client and try to find it in the database.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question