Начал осваивать yii2, возникла проблема со входом на сайт.
Извиняюсь за то, что создаю тему, но уже перевертел и так и этак, перечитал кучу примеров...
По коду нужна именно реализация через dao, а в сети примеры по activeRecord
Ошибка "Setting unknown property: app\models\User::0"
Код: Выделить всё
namespace app\models;
use Yii;
use yii\base\Model;
use yii\web\IdentityInterface;
class User extends Model implements IdentityInterface
{
public $id;
public $username;
public $password;
public $authKey;
public $accessToken;
public $rememberMe = true;
private $_user = false;
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'],
];
}
public function attributeLabels()
{
return [
'username' => 'Имя',
'password' => 'Пароль',
'rememberMe' => 'Запомнить',
];
}
public static function findIdentity($id)
{
$sqlResult = Yii::$app->db->
createCommand('SELECT * FROM staff.v_po_users WHERE id = :id')->
bindValue(':id', $id)->
queryAll();
return (empty($sqlResult)) ? null : $sqlResult;
}
public static function findIdentityByAccessToken($token, $type = null)
{
$sqlResult = Yii::$app->db->
createCommand('SELECT * FROM staff.v_po_users WHERE accessToken = :accessToken')->
bindValue(':accessToken', $token)->
queryAll();
return (empty($sqlResult)) ? null : $sqlResult;
}
public function getId()
{
return $this->id;
}
public function getAuthKey()
{
return $this->authKey;
}
public function validateAuthKey($authKey)
{
return $this->authKey === $authKey;
}
public function validatePassword($attribute, $params)
{
if (!$this->hasErrors()) {
if(!$this->getUser())
{
$this->addError($attribute, 'Неверное имя или пароль');
}
}
}
public function login()
{
if ($this->validate())
{
return Yii::$app->user->login(new static($this->getUser()), $this->rememberMe ? 3600*24*30 : 0);
}
return false;
}
public function getUser()
{
if ($this->_user === false) {
$this->_user = Yii::$app->db->
createCommand('SELECT * FROM staff.v_po_users WHERE username = :username AND password = :password')->
bindValue(':username', $this->username)->
bindValue(':password', $this->password)->
queryAll();
}
return $this->_user;
}