環境: Windows 10 + wampservice + Yii2.0.6
開發工具:phpstorm 9.0.2
Yii ActiveRecord 中的save,我執行後,為什麼資料庫中增加一列,但沒有資料?
官方那個什麼手冊啊,怎麼那麼多問題?我試了幾下都不行,還是說哪裡寫錯了?
我建立了一個模型
namespace app\models;
use yii\db\ActiveRecord;
class user extends ActiveRecord
{
public $username;
public $password;
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public static function tableName()
{
return '{{user}}';
}
在控制器中執行
$user = new user();
$user->username = 'hello';
$user->password = 'world';
$user->save();
資料庫增加一列,但是username和password是空的,只有前面的id,我在資料庫中schema是這樣寫的。
id int(10) unsigned not null primary key auto_increment 。
瀏覽器中執行
localhost:80/yii/web/index.php?r=index/create 執行完沒有報錯,但是查看資料庫,增加一列,但是username和password是空白的。
請問遇到這個問題應該怎麼弄?
回複討論(解決方案)
列印sql看下,insert into的文法,另外需要檢查下,表的結構問題。
遇到這類調試問題,首先應該用print_r($user),查看$user是否是你想要的結果,如果其中的公用屬性欄位正確,那你應該檢查它是否與資料庫中表名和表欄位完全一致。
列印sql看下,insert into的文法,另外需要檢查下,表的結構問題。
遇到這類調試問題,首先應該用print_r($user),查看$user是否是你想要的結果,如果其中的公用屬性欄位正確,那你應該檢查它是否與資料庫中表名和表欄位完全一致。
列印也是沒有報錯的,解決方案就是不要再AR衍生類別裡面定義表的屬性,直接使用save就可以正確儲存。