0
votes

I can quite easily generate a model from MySQL/MariaDB view with Gii, but when I try to generate CRUD, than I recieve the following error message:

The table associated with frontend\models\MyModel must have primary key(s).

See also the discussion in Yii Framework Forum.

2

2 Answers

6
votes

The solution is:

  1. add an ID in the view, e.g. using the CONCAT function,

  2. overwrite the method primaryKey in the generated model.

Here is the code:

public static function primaryKey()
{
    return array('view_id');
}

It is not a standard solution and should be used carefully. Yii does not officially support using active record with views, because different DBMS have different view specs and they usually don't support DB write (2).

1
votes

Working on yii2,
There was issue with primary Key, so I search to add primary key in VIEW TABLE but I can't because -
Create view with primary key?
Adding in a primary key to an SQL view

Then moved to create CRUD on VIEW Table. I view many articles like-


        public static function primaryKey()
        {
            return array('my_view_id');
        }

Worked for me.