Yii Logout Error (Getid() Must Be Static)

I got the following set-up in my frontend\models\User:


class User extends ActiveRecord implements IdentityInterface

  {

      public static function findIdentity($id)

      {

          return static::find($id);

      }

 

      public function getId()

      {

          return $this->id;

      }

 

      public function getAuthKey()

      {

          return $this->authKey;

      }


      public function validateAuthKey($authKey)

      {

          return $this->authKey === $authKey;

      }

  }

Now, when i’m going to site/logout, i get the following error:


The scope method "frontend\models\User::getId()" must be public and static.

If i make this method static, i get (off course) the error that IdentityInterface expects that getId() is non-static. So where does this error come from? As far as i see i think it sees the getId() as a scope.

try public static function getId()

Like I said in my post, I can’t do that, because I implemented the IdentityInterface, resulting in this error:


Cannot make non static method yii\web\IdentityInterface::getId() static in class frontend\models\User

I already found the problem. I edited my findIdentity($id) method to the following (because i’m using a default scope way to find a user):


public static function findIdentity($id)

    {

        return static::find()->where(['user.id' => $id]);

    }

But forgot the one() part, so it was looking in the ActiveQuery for a getId() scope:


public static function findIdentity($id)

    {

        return static::find()->where(['user.id' => $id])->one();

    }

Now it’s working.

Here is an example




<?php


/**

 * UserIdentity represents the data needed to identity a user.

 * It contains the authentication method that checks if the provided

 * data can identity the user.

 */

class UserIdentity extends CUserIdentity

{

	/**

	 * Authenticates a user.

	 * The example implementation makes sure if the email and password

	 * are both 'demo'.

	 * In practical applications, this should be changed to authenticate

	 * against some persistent user identity storage (e.g. database).

	 * @return boolean whether authentication succeeds.

	 */

    private $_id;


    public function authenticate()

    {

        $member=Members::model()->findByAttributes(array('email'=>trim($this->username)));


        if($member===null){

            $this->errorCode=self::ERROR_USERNAME_INVALID;

        }

        elseif($member->password !== md5(trim($this->password))){

            $this->errorCode=self::ERROR_PASSWORD_INVALID;

        }

        elseif($member->activation_date === null){

            $this->errorCode = 11;

        }

        elseif($member->status != 1){

            $this->errorCode = 10;

        }

        else

        {

            $this->_id=$member->id;

            $this->setState('username', $member->username);


            $this->errorCode=self::ERROR_NONE;

        }


        return !$this->errorCode;

    }


    public function getId()

    {

        return $this->_id;

    }


    public function setId($id)

    {

        $this->_id = $id;

    }


}



That’s Yii 1.x ;-), we’re in the Yii 2.0 subforum.

Hah, Sorry…

I didnt look at this :)