測試環境Yii2.0 basic
namespace app\models;useYii;
use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;
clas User extends ActiveRecord implements IdentityInterface {
/*
*若此處聲明瞭
*public $username
* public $password
* 那麼當我們執行模型更新操作的時候$name的數據將沒有映射到數據模型中去,
* 提交數據的時候也會使public 所定義的變量對應的表中的字段爲空。
*
*/
}
經測試 使用protected 和 private 來聲明局部變量是可以正常寫入的。作用域的關係
$model = new User();
$model->username = $username;
$model->password= $password;
假設模型User裏聲明瞭
public $username;
public $password;
那麼我們在給$model->username賦值時就等同於是給User模型中的public變量賦值,而非AR模型本身映射的username字段值。
總結:故不能在模型聲明和數據表字段名一樣的公有變量名