yii2 找到 returns null
yii2 find returns null
为什么 findone() returns 所有 public 属性 都为空。
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;
}
这个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
您应该简单地从您的模型中删除数据库属性:
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. You should NOT redeclare any of the attributes.
阅读更多:http://www.yiiframework.com/doc-2.0/guide-db-active-record.html
为什么 findone() returns 所有 public 属性 都为空。
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;
}
这个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
您应该简单地从您的模型中删除数据库属性:
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. You should NOT redeclare any of the attributes.
阅读更多:http://www.yiiframework.com/doc-2.0/guide-db-active-record.html