Yii Framework Forum: CRUD filter for related object - Yii Framework Forum

Jump to content

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

CRUD filter for related object Rate Topic: ***** 1 Votes

#1 User is offline   Fred 91 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 03-March 12

Posted 16 April 2012 - 07:45 AM


I'm new to Yii and I have a simple problem :

I have two objects with a one to many relation (for example, a FAQ with categories and questions (a question can only have one category)).

I know how to create CRUD (with GII) for categories and questions, but what I would like to do is to have a link on each category (on the categories Gridview) to display only related questions (and not allow questions gridview if no category is specified).

I managed to create the link on the categories gridview :
'label'=>'Manage questions',

But i don't have any idea on how to handle this parameter to display only questions of the chosen catetgory on my questions CRUD.

Can you please help me ?

Thank you very much,


#2 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,630
  • Joined: 16-February 11
  • Location:Japan

Posted 16 April 2012 - 07:12 PM

Hi Frederic,

Because you call 'questions/admin' action with a parameter named 'category_id' ...
// in QuestionsController:
public function actionAdmin()
    $model = new Questions('search');

    if (isset($_GET['category_id']))
        $model->category_id = (int)$_GET['category_id'];

    if (isset($_GET['Questions']))
        $model->attributes = $_GET['Questions'];
    $this->render('admin', array('model' => $model));

Note that this is how the original admin action works:
  • $model is an instance of Questions that holds the searching parameters for the grid.
  • It is initially reset to empty, meaning that the grid initially displays all the questions.
  • When the user input some searching parameters, then they are passed to the controller with $_GET['Questions']. We set them to $model to filter the searching result.

And now, we pass the category_id to the action with $_GET['category_id'].

You can write the action using the action parameter binding ...
public function actionAdmin($category_id = null)
    if ($category_id !== null)
        $model->category_id = (int)$category_id;


#3 User is offline   AussieNeil 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 25-January 13

Posted 03 February 2013 - 02:25 AM


just wanted to say thank you for this it helped solve a problem I have been struggling with.

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