I
I
Ivan Yakushenko2016-07-17 20:28:56
Yii
Ivan Yakushenko, 2016-07-17 20:28:56

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();

    }

Mistake:
Call to a member function save() on null

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Timofeev, 2016-07-18
@kshnkvn

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;
    }

A
Alexey Ukolov, 2016-07-17
@alexey-m-ukolov

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 question

Ask a Question

731 491 924 answers to any question