ActiveRecord Insert with only ID Attribute

I try to insert new row with ActiveRecord mechanism, but fail even with a very simple table structure. Following are the detail of my table, model and controller.

I have a table in MySQL 5.6 with the following DDL.

[sql]

CREATE TABLE testing (

id int(11) NOT NULL AUTO_INCREMENT,

name varchar(25) DEFAULT NULL,

PRIMARY KEY (id)

) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1

[/sql]

And I have a model class named Testing.php.




<?php

namespace app\models;


use yii\db\ActiveRecord;


class Testing extends ActiveRecord

{

    public $id;

    public $name;


	//for active record

    /**

     * @return string the name of the table associated with this ActiveRecord class.

     */	

    public static function tableName()

    {

        return 'testing';

    }	

	

    public static function getDb()

    {

        return \Yii::$app->db; 

    }	

}

?>



and a controller called TestingController.php.




<?php

namespace app\controllers;


use Yii;


use yii\web\Controller;

use app\models\Testing;


class TestingController extends Controller

{

	public function actionInsert()

	{

			$model = new Testing();

			$model->name = "Name Test";

			$model->save(false);

			

			return null;

	}

}

?>



When I execute the Insert action, only "id" is filled. Attached with the post is the image showing the log detail and data.

What is wrong with my code? Is there any configuration that I need to check?

Thanks,

Dino

delete variabel ‘id’, ‘name’ in model Testing and try again.

Hi Thang, it works now. Thanks!

But why they should be removed?

Regards,

Dino

When you publicly declare $id and $name variables in the model, they have priority on db’s fields, called internally in the model.

Ah, I see. Thank you Fabrizio!