Symfony管理生成器,链接到过滤结果

时间:2011-05-31 17:16:11

标签: symfony1 filter admin-generator

我有一个管理生成器用于以下模型:

#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}

我已经创建了2页,每页都对应

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

现在我想在作者视图中找到他的书的链接。 最好的方法是什么? 我的尝试是预先选择过滤器的自定义链接,但我不知道该怎么做。

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

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

由于

1 个答案:

答案 0 :(得分:5)

本讲座回答了您的问题(幻灯片39和40): http://www.slideshare.net/jcleveley/working-with-the-admin-generator

你的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')); 
  }
}

注意案例:字段名称与架构中定义的案例相匹配。例如:如果模式中有authorId,则必须在上面的函数中编写authorId(第5行)。如果架构中有author_id,则必须在函数中编写author_id。此外,模块名称始终为小写,带下划线(例如:schema : MyBooks -> module name: my_books)。

在您的generator.yml


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