title: Yii 2.0 login from database not working
body: <p>Right i am working on yii 2.0 trying to amend the login system. It works when the users are just a hand coded array. Instead i want to make this work from a database.</p>
  
  <p>I will show you what the initial Model looks like:</p>
  
  <pre><code>&lt;?php
  
  namespace app\models;
  
  class User extends \yii\base\Object implements \yii\web\IdentityInterface
  {
  public $id;
  public $username;
  public $password;
  public $authKey;
  public $accessToken;
  
  private static $users = [
      '100' =&gt; [
          'id' =&gt; '100',
          'username' =&gt; 'admin',
          'password' =&gt; 'admin',
          'authKey' =&gt; 'test100key',
          'accessToken' =&gt; '100-token',
      ],
      '101' =&gt; [
          'id' =&gt; '101',
          'username' =&gt; 'demo',
          'password' =&gt; 'demo',
          'authKey' =&gt; 'test101key',
          'accessToken' =&gt; '101-token',
      ],
  ];
  
  /**
   * @inheritdoc
   */
  public static function findIdentity($id)
  {
      return isset(self::$users[$id]) ? new static(self::$users[$id]) : null;
  }
  
  /**
   * @inheritdoc
   */
  public static function findIdentityByAccessToken($token, $type = null)
  {
      foreach (self::$users as $user) {
          if ($user['accessToken'] === $token) {
              return new static($user);
          }
      }
  
      return null;
  }
  
  /**
   * Finds user by username
   *
   * @param  string      $username
   * @return static|null
   */
  public static function findByUsername($username)
  {
      foreach (self::$users as $user) {
          if (strcasecmp($user['username'], $username) === 0) {
              return new static($user);
          }
      }
  
      return null;
  }
  
  /**
   * @inheritdoc
   */
  public function getId()
  {
      return $this-&gt;id;
  }
  
  /**
   * @inheritdoc
   */
  public function getAuthKey()
  {
      return $this-&gt;authKey;
  }
  
  /**
   * @inheritdoc
   */
  public function validateAuthKey($authKey)
  {
      return $this-&gt;authKey === $authKey;
  }
  
  /**
   * Validates password
   *
   * @param  string  $password password to validate
   * @return boolean if password provided is valid for current user
   */
  public function validatePassword($password)
  {
      return $this-&gt;password === $password;
  }
  }
  </code></pre>
  
  <p>okay so as you can see it is working on the hand coded array called $users. So i have made a table called "Users" and made the columns id username password authkey and accessToken.</p>
  
  <p>Hoping that it would do the same thing with a database table, however i am getting an error when i try to log in. This is my new code</p>
  
  <pre><code> &lt;?php
  
  namespace app\models;
  
  /**
   * This is the model class for table "Cases".
   *
   * @property integer $id
   * @property string $username
   * @property string $password
   * @property string $authkey
   * @property string $accessToken
   */
  class User extends \yii\db\ActiveRecord
  {
  
  
  /**
   * @inheritdoc
   */
  public static function tableName()
  {
      return 'users';
  }
  
  public function rules()
  {
      return [
          [['id','username','password','authkey','accessToken'], 'required'],
          [['id'], 'integer'],      
      ];
  }
  
  /**
   * @inheritdoc
   */
  public function attributeLabels()
  {
      return [
          'id' =&gt; 'id',
          'username' =&gt; 'username',
          'password' =&gt; 'password',
          'authkey' =&gt; 'authkey',
          'accessToken' =&gt; 'accessToken',
      ];
  }
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  /**
   * @inheritdoc
   */
  public static function findIdentity($id)
  {
      return isset(self::$users[$id]) ? new static(self::$users[$id]) : null;
  }
  
  /**
   * @inheritdoc
   */
  public static function findIdentityByAccessToken($token, $type = null)
  {
      foreach (self::$users as $user) {
          if ($user['accessToken'] === $token) {
              return new static($user);
          }
      }
  
      return null;
  }
  
  /**
   * Finds user by username
   *
   * @param  string      $username
   * @return static|null
   */
  public static function findByUsername($username)
  {
      foreach (self::$Users as $user) {
          if (strcasecmp($user['username'], $username) === 0) {
              return new static($user);
          }
      }
  
      return null;
  }
  
  /**
   * @inheritdoc
   */
  public function getId()
  {
      return $this-&gt;id;
  }
  
  /**
   * @inheritdoc
   */
  public function getAuthKey()
  {
      return $this-&gt;authKey;
  }
  
  /**
   * @inheritdoc
   */
  public function validateAuthKey($authKey)
  {
      return $this-&gt;authKey === $authKey;
  }
  
  /**
   * Validates password
   *
   * @param  string  $password password to validate
   * @return boolean if password provided is valid for current user
   */
  public function validatePassword($password)
  {
      return $this-&gt;password === $password;
  }
  }
  </code></pre>
  
  <p>The error message i am getting when i try to log in is "Access to undeclared static property: app\models\User::$Users".</p>
  
  <p>If you need to see my LoginForm Model and Controller action i will post them underneath here.</p>
  
  <pre><code>public function actionLogin()
  {
      if (!\Yii::$app-&gt;user-&gt;isGuest) {
          return $this-&gt;goHome();
      }
  
      $model = new LoginForm();
      if ($model-&gt;load(Yii::$app-&gt;request-&gt;post()) &amp;&amp; $model-&gt;login()) {
          return $this-&gt;goBack();
      } else {
          return $this-&gt;render('login', [
              'model' =&gt; $model,
          ]);
      }
  }
  </code></pre>
  
  <p>and LoginForm model is:</p>
  
  <pre><code>&lt;?php
  
  namespace app\models;
  
  use Yii;
  use yii\base\Model;
  
  /**
  * LoginForm is the model behind the login form.
  */
  class LoginForm extends Model
  {
  public $username;
  public $password;
  public $rememberMe = true;
  
  private $_user = false;
  
  
  /**
   * @return array the validation rules.
   */
  public function rules()
  {
      return [
          // username and password are both required
          [['username', 'password'], 'required'],
          // rememberMe must be a boolean value
          ['rememberMe', 'boolean'],
          // password is validated by validatePassword()
          ['password', 'validatePassword'],
      ];
  }
  
  /**
   * Validates the password.
   * This method serves as the inline validation for password.
   *
   * @param string $attribute the attribute currently being validated
   * @param array $params the additional name-value pairs given in the rule
   */
  public function validatePassword($attribute, $params)
  {
      if (!$this-&gt;hasErrors()) {
          $user = $this-&gt;getUser();
  
          if (!$user || !$user-&gt;validatePassword($this-&gt;password)) {
              $this-&gt;addError($attribute, 'Incorrect username or password.');
          }
      }
  }
  
  /**
   * Logs in a user using the provided username and password.
   * @return boolean whether the user is logged in successfully
   */
  public function login()
  {
      if ($this-&gt;validate()) {
          return Yii::$app-&gt;user-&gt;login($this-&gt;getUser(), $this-&gt;rememberMe ? 3600*24*30 : 0);
      } else {
          return false;
      }
  }
  
  /**
   * Finds user by [[username]]
   *
   * @return User|null
   */
  public function getUser()
  {
      if ($this-&gt;_user === false) {
          $this-&gt;_user = User::findByUsername($this-&gt;username);
      }
  
      return $this-&gt;_user;
  }
  }
  </code></pre>
  
  <p>Can any one advise what i should do , once again the error i get is:</p>
  
  <p>Access to undeclared static property: app\models\User::$Users </p>
  
  <p>and this error corresponds to this section of code</p>
  
  <pre><code> foreach (self::$Users as $user) {
          if (strcasecmp($user['username'], $username) === 0) {
              return new static($user);
          }
      }
  </code></pre>