Yii ActiveRecord 不填充父对象
Yii ActiveRecord does not populate the parent object
我正在尝试通过 属性 获取活动记录。
$user = self::find()->where(["username" => $username])->one();
当我检查方法 one()
时,我发现 BaseActiveRecord::populateRecord($record, $row)
从数据库中获取行。
正如您在图片上看到的那样,该行已检索到,但由于某种原因,我的对象尚未填充。
我尝试为字段添加设置器,结果是一样的。
有人可以帮我解决这个问题吗?
编辑:
我检查了 BaseActiveRecord
class:
public static function populateRecord($record, $row)
{
$columns = array_flip($record->attributes());
foreach ($row as $name => $value) {
if (isset($columns[$name])) {
$record->_attributes[$name] = $value;
} elseif ($record->canSetProperty($name)) {
$record->$name = $value;
}
}
$record->_oldAttributes = $record->_attributes;
$record->_related = [];
$record->_relationsDependencies = [];
}
当然,$columns[$name]
已经设置好了。
编辑2:
这是我的整个 ActiveRecord class:
<?php
namespace app\models\database;
use Yii;
use yii\base\Exception;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;
class Admin extends ActiveRecord implements IdentityInterface
{
/**
* @var int
*/
public int $id = 0;
/**
* @var string
*/
public string $username = '';
/**
* @var string
*/
public string $password = '';
/**
* @var string
*/
public string $auth_key = '';
/**
* @var string
*/
public string $access_token = '';
/**
* {@inheritdoc}
*/
public static function tableName(): string
{
return 'admin';
}
/**
* @param int|string $id
* @return Admin|IdentityInterface|null
*/
public static function findIdentity($id)
{
$user = self::find()->where(["id" => $id])->one();
if ($user) {
return null;
}
return new static($user);
}
/**
* @param mixed $token
* @param null $type
* @return IdentityInterface|static|null
*/
public static function findIdentityByAccessToken($token, $type = null)
{
$user = self::find()->where(["accessToken" => $token])->one();
if ($user) {
return null;
}
return new static($user);
}
public static function findByUsername($username): ?Admin
{
$user = self::find()->where(["username" => $username])->one();
if ($user) {
return null;
}
return new static($user);
}
public function validateAuthKey($authKey): bool
{
return $this->auth_key === $authKey;
}
/**
* @throws Exception
*/
public function validatePassword($password): bool
{
return $this->password === Yii::$app->getSecurity()->generatePasswordHash($password);;
}
/**
* @param bool $insert
* @return bool
* @throws Exception
*/
public function beforeSave($insert): bool
{
if (parent::beforeSave($insert)) {
if ($this->isNewRecord) {
$this->auth_key = Yii::$app->security->generateRandomString();
}
return true;
}
return false;
}
public function getAccessToken(): string
{
return $this->access_token;
}
public function getId(): int
{
return $this->id;
}
public function getAuthKey(): ?string
{
return $this->auth_key;
}
}
ActiveRecord
将属性存储在私有 属性 $_attributes
and exposes them as virtual properties via __get()
and __set()
. It works as long as you don't define real property with that name (which you did). In that case you either need to remove these properties (for example public string $username = '';
), or use getAttribute()
中以获取属性值(不太方便):
$username = $user->getAttribute('username');
我正在尝试通过 属性 获取活动记录。
$user = self::find()->where(["username" => $username])->one();
当我检查方法 one()
时,我发现 BaseActiveRecord::populateRecord($record, $row)
从数据库中获取行。
正如您在图片上看到的那样,该行已检索到,但由于某种原因,我的对象尚未填充。
我尝试为字段添加设置器,结果是一样的。
有人可以帮我解决这个问题吗?
编辑:
我检查了 BaseActiveRecord
class:
public static function populateRecord($record, $row)
{
$columns = array_flip($record->attributes());
foreach ($row as $name => $value) {
if (isset($columns[$name])) {
$record->_attributes[$name] = $value;
} elseif ($record->canSetProperty($name)) {
$record->$name = $value;
}
}
$record->_oldAttributes = $record->_attributes;
$record->_related = [];
$record->_relationsDependencies = [];
}
当然,$columns[$name]
已经设置好了。
编辑2:
这是我的整个 ActiveRecord class:
<?php
namespace app\models\database;
use Yii;
use yii\base\Exception;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;
class Admin extends ActiveRecord implements IdentityInterface
{
/**
* @var int
*/
public int $id = 0;
/**
* @var string
*/
public string $username = '';
/**
* @var string
*/
public string $password = '';
/**
* @var string
*/
public string $auth_key = '';
/**
* @var string
*/
public string $access_token = '';
/**
* {@inheritdoc}
*/
public static function tableName(): string
{
return 'admin';
}
/**
* @param int|string $id
* @return Admin|IdentityInterface|null
*/
public static function findIdentity($id)
{
$user = self::find()->where(["id" => $id])->one();
if ($user) {
return null;
}
return new static($user);
}
/**
* @param mixed $token
* @param null $type
* @return IdentityInterface|static|null
*/
public static function findIdentityByAccessToken($token, $type = null)
{
$user = self::find()->where(["accessToken" => $token])->one();
if ($user) {
return null;
}
return new static($user);
}
public static function findByUsername($username): ?Admin
{
$user = self::find()->where(["username" => $username])->one();
if ($user) {
return null;
}
return new static($user);
}
public function validateAuthKey($authKey): bool
{
return $this->auth_key === $authKey;
}
/**
* @throws Exception
*/
public function validatePassword($password): bool
{
return $this->password === Yii::$app->getSecurity()->generatePasswordHash($password);;
}
/**
* @param bool $insert
* @return bool
* @throws Exception
*/
public function beforeSave($insert): bool
{
if (parent::beforeSave($insert)) {
if ($this->isNewRecord) {
$this->auth_key = Yii::$app->security->generateRandomString();
}
return true;
}
return false;
}
public function getAccessToken(): string
{
return $this->access_token;
}
public function getId(): int
{
return $this->id;
}
public function getAuthKey(): ?string
{
return $this->auth_key;
}
}
ActiveRecord
将属性存储在私有 属性 $_attributes
and exposes them as virtual properties via __get()
and __set()
. It works as long as you don't define real property with that name (which you did). In that case you either need to remove these properties (for example public string $username = '';
), or use getAttribute()
中以获取属性值(不太方便):
$username = $user->getAttribute('username');