Heim  >  Artikel  >  Backend-Entwicklung  >  Yii ActiveRecord 中的save,小弟我执行后,为什么数据库中增加一列,但没有数据

Yii ActiveRecord 中的save,小弟我执行后,为什么数据库中增加一列,但没有数据

WBOY
WBOYOriginal
2016-06-13 12:27:18988Durchsuche

Yii ActiveRecord 中的save,我执行后,为什么数据库中增加一列,但没有数据?
环境: 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是否是你想要的结果,如果其中的公共属性字段正确,那你应该检查它是否与数据库中表名和表字段完全一致。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn