parent-child relation, view page

Hi,

I am looking for some about how to implement the following thing with yii2:

Lets imagine we have the following structure:

  • authors table.

  • Books table

Each author -> has written 1 or more books.

What I want to do is to display on a view, the details of an author, and under it, a gridview of all the books it has done, and the control buttons to create/ remove new books for that current author…

How I should retrieve the data of the books? From the same controller for Authors?

I have already implemented the table relations between authors and books, in the Author’s model

Thanks in advance, any help is welcome!!

Hi, see this demo and see Expanded Column Feature.

http://demos.krajee.com/grid-demo

Another way is to show all authors in your grid and link each author to page such as ?r=author/books&id=42 to show all books from author ID 42 (another grid).

On the Author’s controller, I modified the “view” action, so I could a “books” activeDataprovider, and then filtering it by “author id”:

public function actionView($id) { //get current author $author=$this->findModel($id);

$books_search = new BookSearch();





//get the related books for current author

$written_books = $books_search->search([‘bookSearch’=>[‘id_author’=>$id]]);

    return $this->render('view', [


    'author' => $author,


    'books_search'=> $books_search,


    'books'=>$written_books,


]);

}

Then I could create on the Author’s view, a gridview with the books written by the current author

@holylander you Could also give Giiant a try …

It should generate your required views automatically