2012-09-17 77 views
9

我試圖在奏鳴曲管理包中添加操作。我改變了我的管理類:在SonataAdminBundle中添加操作

protected function configureListFields(ListMapper $listMapper) 
{ 
    $listMapper 
     ->addIdentifier('id') 
     ->add('name')   
     // add custom action links 
     ->add('_action', 'actions', array(
      'actions' => array(
       'view' => array(), 
       'calculate' => array('template' => 'myappMyBundle:Admin:list__action_calculate.html.twig'), 
       'edit' => array(), 
      ) 
     )) 
    ; 
} 

protected function configureSideMenu(MenuItemInterface $menu, $action, Admin $childAdmin = null) 
{ 
    if (!$childAdmin && !in_array($action, array('edit'))) { 
     return; 
    } 
    $admin = $this->isChild() ? $this->getParent() : $this; 
    $id = $admin->getRequest()->get('id'); 
    $menu->addChild('calculate', array('uri' => 'http://google.com?id=' . $id)); 
} 

,並把一個叫list__action_calculate.html.twig在SRC/MYAPP/MyBundle /資源/視圖/管理/模板:

{% if admin.isGranted('EDIT', object) and admin.hasRoute('edit') %} 
<a href="{{ admin.generateObjectUrl('calculate', object) }}" class="calculate_link" title="{{ 'action_calculate'|trans({}, 'SonataAdminBundle') }}"> 
    <img src="{{ asset('bundles/sonataadmin/famfamfam/page_white_edit.png') }}" alt="{{ 'action_calculate'|trans({}, 'SonataAdminBundle') }}" /> 
</a> 
{% endif %} 

但我得到了symfony的這個錯誤:

An exception has been thrown during the rendering of a template 
("unable to find the route `mysite.mybundle.admin.myentity.calculate`") 
in "SonataAdminBundle:CRUD:list.html.twig" 

我錯過了什麼? 在文檔中是否有此頁面的Doc

回答

6

終於明白了!

在管理類:

protected function configureRoutes(RouteCollection $collection) 
{ 
    $collection->add('calculate'); 

} 

# Override to add actions like delete, etc... 
public function getBatchActions() 
{ 
    // retrieve the default (currently only the delete action) actions 
    $actions = parent::getBatchActions(); 

    // check user permissions 
    if($this->hasRoute('edit') && $this->isGranted('EDIT') && $this->hasRoute('delete') && $this->isGranted('DELETE')) 
    { 
     // define calculate action 
     $actions['calculate']= array ('label' => 'Calculate', 'ask_confirmation' => true); 

    } 

    return $actions; 
} 

protected function configureListFields(ListMapper $listMapper) 
{ 
    $listMapper 
     ->addIdentifier('id') 
     ->add('name')   
     // add custom action links 
     ->add('_action', 'actions', array(
      'actions' => array(
       'view' => array(), 
       'calculate' => array('template' => 'chemoinfoEdrugBundle:CRUD:list__action_calculate.html.twig'), 
       'edit' => array(), 
      ) 
     )) 
    ; 
} 

,並在管理控制器:

public function batchActionCalculate(ProxyQueryInterface $selectedModelQuery) 
{ 
    ... 
} 

,並在/ src目錄/ mysite的/ mybundle /資源/視圖/ CRUD:

{% if admin.isGranted('EDIT', object) and admin.hasRoute('edit') %} 
<a href="{{ admin.generateObjectUrl('calculate', object) }}" class="calculate_link" title="{{ 'action_calculate'|trans({}, 'SonataAdminBundle') }}"> 
    <img src="{{ asset('bundles/sonataadmin/famfamfam/calculator.png') }}" alt="{{ 'action_calculate'|trans({}, 'SonataAdminBundle') }}" /> 
</a> 
{% endif %} 
+0

官方文檔:[批量操作](http://sonata-project.org/bundles/admin/2-2/doc/reference/batch_actions.html) –

+0

新文檔是go od,感謝您的更新。 – user1254498