Search code examples
phpyiiyii2yii2-basic-app

Generate static typing of Column name in yii active record


I have a yii basic app where test is database table id,name

and models

class Test extends ActiveRecord
{

    //public $name;
    //public $id;

    public static function tableName()
    {
        return '{{test}}';
    }
}

when i declare a $name/$id field in Test class.It not binding to database column name??

Is there any way to declare public $name; and get the data/or set the data by static typing ?

Like a declare public $name; and in controller

    $test=new Test();

    $test->name='test2';
    $test->save(); //not working

Solution

  • You must not declare properties of ActiveRecord class of the same name as mapped database columns.

    Mapped properties are fetched automatically and you have got access to them using standard getters and setters.

    If you declare property like in your example property values are not fetched from database.