Show Items List Under Category Details

Take the following tables:

  • Categories (id, name)

  • Books (id, title, author, category_id)

Using Gii and crud I can setup the basics. I know how to use relational data, so I can display the category name on the book details page, and link from there to the specific category.

What I would like it to display the book list under the category details view. So if I open the category list and click on a category (e.g. fiction), the fiction view should display (a) the fiction details (category id, name), and below a list of fiction books, with links to the book details views.

Is there a tutorial somewhere that can teach me setting this up?

Test this:




$categories = Categories::model()->with('books')->findAll();


foreach ($categories as $categorie):

   	echo $categorie->name;

    	foreach ($categorie->books as $book)

           	echo $book->title;

endforeach;



You can do it as in view.php of the category views




$books=Books::model()->findAllByAttributes(array(

'category_id'=>$model->id // $model= category model

));


// or if you have book as relation in category

$books=$model->book

// then do as

if($books){

 foreach($books as $book){

 echo 'Name : '.$book->name;

}

}



Thanks! This works great.