View Instead Of Table

dear all,

well, assume that i have a simple relationship like this:

User(id, name), Role(id, role), and UserRole(id, user_id, role_id).

then, to avoid JOINING all those tables, then a view is design to show all those table’s fields, VUserRole.

then, i make a table using CGridView. my question is, the CGridView is not search-able.

when i tried to generate it’s CRUD, i failed, it said that it has no primary key. yes, of course it’s a view and not a table.

so, there is a view (VUserRole) behind the CGridVIew, is it possible to make it’s record search-able, like if we make it from table,.?


i make a test like this.

assume that i also make another view just simply show all User’s fields, named VUser.

both User table and VUser models are generated by using Gii, and everything is fine.

i also generate CRUD for User’s model.

i simply change the model class in the user/admin action.


new User('search') 


new VUser('search') 

and,… the search form at the CGridView is not search-able.

i am still not sure if we can use (MySQL) database view behind the CGridVIew,.

yes, this is a lot of painful if we have to make our own search method (simple example).

for a complex query, i think a database view is a great option.

hope further major release can do this one :)

thanks, Yii,.!

actually you can use MySql views for this purpose and yii doesn’t differ views from tables.

you can generate models by gii but generating CRUD need the table to have a primary key so you can create your model by gii and do the rest (controller and views) by yourself or otherwise you can simply add a primary key in your model and then use gii to generate CRUD for you then remove parts that you don’t need

add this to your view Model and then try gii CRUD generator again

public function primaryKey()


    return 'primary_key_column_name_here';   


you also need to change this part to make it work: