Tekko Tekko - 8 months ago 120
MySQL Question

Yii2 Add ActiveRecord Model Attribute

In Yii2 I have an ActiveRecord model already created, based on a MySQL table, but I had to add a new column to the table, and I don't know which change should I do to the Yii2 model in order to be able to use this new column.

I tried adding "public $columnname" to the class, but it allows me to see the value on the column, but I cannot modify it (maybe there is something missing, like a "setAttribute" function)

What do you think?


After you add a column to a table, you need to mark this attribute at least as safe in model rules:

public function rules()
    return [
        // other rules...
        [['yourNewColumn'], 'safe'],

and now this attribute will be consider safe for assigning value to it. Explicit declaration public $columnname not needed because yii2 parse table schema for existing fields