Save in YiiActiveRecord. Why does the database add a column but no data after I execute it?

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.