Yii Framework Forum: View Instead Of Table - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

View Instead Of Table Rate Topic: -----

#1 User is offline   mariosimaremare 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 17-June 10

Posted 08 February 2013 - 05:08 AM

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,.?

thanks.
0

#2 User is offline   mariosimaremare 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 17-June 10

Posted 08 February 2013 - 05:36 AM

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.
from
new User('search') 

to
new VUser('search') 

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

#3 User is offline   mariosimaremare 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 17-June 10

Posted 08 February 2013 - 07:19 AM

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,.!
0

#4 User is offline   Reza m 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 148
  • Joined: 07-March 12

Posted 08 February 2013 - 07:31 AM

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';   
}



View Postmariosimaremare, on 08 February 2013 - 05:36 AM, said:

i simply change the model class in the user/admin action.
from
new User('search') 

to
new VUser('search') 

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


you also need to change this part to make it work:
if(isset($_GET['VUser']))
	$model->attributes=$_GET['VUser'];

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users