Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 ActiveRecord saves null values instead of real values

I'm starting with Yii2 and want to save something to my database. this is my model:

class Course extends ActiveRecord
{
    public $name;

    public function rules()
    {
        return [
            [['name'], 'integer']
        ];
    }
}

and I call it like this:

$Course = new Course();
$Course->name = 44;
$Course->save();

This is a simple code but it insert null in my database. my table just has a name column as int. What is the solution? I have read THIS question but his problem is not the same as me

like image 797
Leo Avatar asked Oct 18 '25 09:10

Leo


1 Answers

If name is the real column name from your database you need to remove

public $name;

from your ActiveRecord class. You can not have model properties of the same name as the database columns declared in the class because ActiveRecord won't work.

like image 194
Bizley Avatar answered Oct 21 '25 23:10

Bizley