[英]yii2 find returns null
Why does findone() returns all public property as null. 为什么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;
}
This returns 这返回
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
You should simply remove db attributes from your model : 您应该只从模型中删除db属性:
class User extends \yii\db\ActiveRecord
{
public static function findByUsername($username)
{
....
Yii automatically defines an attribute in Active Record for every column of the associated table. Yii会在Active Record中为关联表的每一列自动定义一个属性。 You should NOT redeclare any of the attributes . 您不应该重新声明任何属性 。
Read more : http://www.yiiframework.com/doc-2.0/guide-db-active-record.html 了解更多: http : //www.yiiframework.com/doc-2.0/guide-db-active-record.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.