Save in YiiActiveRecord. Why does the database add a column but no data after I execute it? Environment: Windows 10 + wampservice + Yii2.0.6
Development Tool: phpstorm 9.0.2
In Yii ActiveRecord, after I run the save command, why does the database add a column but no data?
What is the official manual? why are there so many questions? I tried a few times and I couldn't do it, or did I say something wrong?
I created a model.
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 }}';
}
Run
$ User = new user ();
$ User-> username = 'hello ';
$ User-> password = 'world ';
$ User-> save ();
Add a column to the database, but the username and password are empty, with only the previous id. in the database, schema is written like this.
Id int (10) unsigned not null primary key auto_increment.
Execute in the browser
Localhost: 80/yii/web/index. php? R = index/create: no error is reported after execution. However, when you view the database, add a column, but the username and password are blank.
How can I solve this problem?
Reply to discussion (solution)
Print the SQL statement and check the insert into syntax. Also, check the structure of the table.
When encountering such debugging problems, you should first use print_r ($ user) to check whether $ user is the result you want. if the public attribute field is correct, check whether it is consistent with the table name and table field in the database.
Print the SQL statement and check the insert into syntax. Also, check the structure of the table.
When encountering such debugging problems, you should first use print_r ($ user) to check whether $ user is the result you want. if the public attribute field is correct, check whether it is consistent with the table name and table field in the database.
There is no error in printing. the solution is not to define the table attributes in the AR derived class. you can directly save the table by using save.