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

5

我有一个针对以下模型的管理生成器:

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

我已生成了两个页面,每个页面对应一个表格。
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个回答

5
这个讲座回答了你的问题(第39和40张幻灯片):http://www.slideshare.net/jcleveley/working-with-the-admin-generator 在你的actions.class.php中添加以下内容:

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,则在上面的函数(第5行)中必须写authorId。如果模式中有author_id,则必须在函数中写author_id。此外,模块名称始终是小写带下划线(例如:schema:MyBooks -> 模块名称:my_books)。

In your generator.yml


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


太好了,谢谢你修复它,我通常按照下划线符号的约定使用,我忘记还有其他符号可用。 - Arend
好主意,到目前为止我一直在使用更加hack的方法来实现这个。 - benlumley

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接