Search code examples
symfony1filteradmin-generator

Symfony admin generator, link to filtered result


i have an admin generator for the folowing model:

#schema.yml
Author:
  columns:
    name: { type: string(255), notnull: true }

Book:
  columns:
    authorId: { type: integer, notnull: true }
    title: { type: string(512), notnull: true }
    content: { type: string(512), notnull: true }
  relations:
    Author: { onDelete: CASCADE, local: authorId, foreign: id, foreignAlias: Books}

I have gererated 2 pages with corresponding with each table

php symfony doctrine:generate-admin backend Author
php symfony doctrine:generate-admin backend Book

Now i want in the author view a link to his books. What is the best way to do that ? My try is a custom link that pre-selects filters, but i dont know how to do that.

#generator.yml for Author
# ...
    config:
      actions: ~
      fields:
      list:
        display: [id, name, _booksLink]
      filter:  ~
      form:    ~
      edit:    ~
      new:     ~

and

<!-- modules/author/templates/_booksLink.php -->
<a href="<?= link_to('book?author_id='.$author->getId()) ?>"><!-- how to select filter? -->
  See <?= $author->getName() ?> books
</a>

Thanks


Solution

  • This lecture answers your question (slide 39 and 40): http://www.slideshare.net/jcleveley/working-with-the-admin-generator

    in your actions.class php add:

    
    class AuthorActions extends AutoAuthorActions
    {
      public function executeViewBooks($request) { 
        $this->getUser()->setAttribute( 'book.filters',   
        array('authorId' => $request->getParameter('id')), 'admin_module' ); 
        $this->redirect($this->generateUrl('book')); 
      }
    }
    

    Note about the case: The field name matches the case defined in the schema. Eg: if you have authorId in the schema, you must write authorId in the function above (line 5). If you have author_id in the schema, you must write author_id in the function. Also, The module names are always lowercase with underscores (eg: schema : MyBooks -> module name: my_books).

    In your generator.yml

    
    list: 
      object_actions:
        _edit: ~
        _delete: ~
        viewPhones: { label: View books, action: viewBooks }