a simple model just cant save after setattributes

my questions are comment in the following code at (1) and (2),may someone help me~

##########

namespace common\models;

use yii;

use components\base\BaseActiveRecord;

use yii\base\NotSupportedException;

use yii\db\ActiveRecord;

use yii\base\Model;

use yii\web\IdentityInterface;

class Loan extends BaseActiveRecord

{

public $user_id;


public $phone;


public $amount;


public $real_name;


public $ident_card_id;





public static function tableName()


{


    return 'loan';


}

/*public function __construct($scenario = ‘insert’)

{

//some stuff


parent::__construct($scenario);

}*/

/**


 * @inheritdoc


*/


public function behaviors()


{


    return [


              'timestamp' => [


                                'class' => 'yii\behaviors\TimestampBehavior',


                                'attributes' => [


                                                   ActiveRecord::EVENT_BEFORE_INSERT => [


                                                        'create_time'


                                                   ]


                                                ]


                             ]


           ];


}





public function attributeLabels()


{


    return [


               'user_id' => '用户ID',


               'phone' => '电话',


               'amount' => '金额',


               'real_name' => '真实姓名',


               'ident_card_id' => '身份验证号码',


           ];


}











public static function create($attributes)


{ 


   /**


    *


    * @var Loan $loan


    */


   $loan = new static();


   $loan->setAttributes($attributes,false);





   Yii::info("After set real_name: $loan->real_name");//(1)seems value is set ok in log





   if ($loan->save())


   {


      Yii::info(&quot;The loanInfo is inserted of username: &#036;loan-&gt;real_name&quot;);//(2)real_name is also ok in log, but save into database failed, none of user_id/real_name/ident_card_id/phone/amount is saved.<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />?


      return &#036;loan;


   }


   else


   {


       return null;





   }


}








public function rules()


{


    return [


         ['user_id', 'safe'],


          ['user_id', 'required'],





          ['real_name', 'safe'],


          ['real_name', 'required'],





       ['ident_card_id', 'safe'],


          ['ident_card_id', 'required'],





          ['phone', 'safe'],


          ['phone', 'required'],





         ['amount', 'safe'],


          ['amount', 'required'],


    ];


}

}

What are $attributes values in input in create() ?

It is just the public values in class Loan, which are user_id,real_name,ident_card_id,phone,amount

It is just the public values in class Loan, which are user_id,real_name,ident_card_id,phone,amount

Your mistake is declaring public variables at top of Loan class.

Delete all declares, otherwise you will set local variables and not database variables

So:

class Loan extends BaseActiveRecord

{

[s]

public $user_id;

public $phone;

public $amount;

public $real_name;

public $ident_card_id;

[/s]

Many thanks, it works。