Yii ActiveRecord 中的save,我執行後,為什麼資料庫中增加一列,但沒有資料?

來源:互聯網
上載者:User
環境: 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就可以正確儲存。
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.