为什么findone()将所有公共属性返回为null。
class User extends \yii\db\ActiveRecord
{
public $id;
public $username;
public $password;
public $authKey;
public $accessToken;
/**
* Finds user by username
*
* @param string $username
* @return static|null
*/
public static function findByUsername($username)
{
$result = static::findOne(['username' => $username]);
var_dump($result);
return $result;
}
返回
object(app\models\User)[81]
public 'id' => null
public 'username' => null
public 'password' => null
public 'authKey' => null
public 'accessToken' => null
private '_attributes' (yii\db\BaseActiveRecord) =>
array (size=6)
'id' => int 1
'username' => string 'admin' (length=5)
'password' => string '123456' (length=6)
'auth_key' => string 'jkkk' (length=4)
'created' => null
'modified' => null
答案 0 :(得分:4)
您只需从模型中删除db属性:
class User extends \yii\db\ActiveRecord
{
public static function findByUsername($username)
{
....
Yii会自动在Active Record中为关联表的每一列定义一个属性。 您不应重新声明任何属性。
了解详情:http://www.yiiframework.com/doc-2.0/guide-db-active-record.html