Reputation: 2040
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.
Upvotes: 0
Views: 1654
Reputation: 4509
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.
Upvotes: 1
Reputation: 2040
The solution is:
add an ID in the view, e.g. using the CONCAT function,
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).
Upvotes: 6