簡體   English   中英

yii2 find返回null

[英]yii2 find returns 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;
    }

這返回

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

您應該只從模型中刪除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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM