2016-08-01 92 views
2

我試圖在奏鳴曲管理器上添加新的批量操作。但是,當我實現我的項目例如在Sonata-Project,它說:在奏鳴曲管理器上創建批量操作管理

注意:未定義指數:translation_domain

如何解決呢?

某些代碼件:

AdminClass.php

<?php 
use Sonata\AdminBundle\Admin\Admin; 

class AdminClass extends Admin { 
    //Other variables, functions 
    public function getBatchActions() { 
     // retrieve the default batch actions (currently only delete) 
     $actions = parent::getBatchActions(); 
     if (
      $this->hasRoute('edit') && $this->isGranted('EDIT') && 
      $this->hasRoute('delete') && $this->isGranted('DELETE') 
     ) { 
      $actions['merge'] = array(
       'label' => 'Мэйл илгээх', 
       'ask_confirmation' => true 
      ); 

     } 

     return $actions; 
    } 

    /*protected function configureRoutes(RouteCollection $collection) { 
     $collection->add('sendAction'); 
     $collection->add('send-mail', $this->getRouterIdParameter().'/send-mail'); 
    }*/ 
} 

CRUDController.php

<?php 

use Sonata\AdminBundle\Controller\CRUDController as BaseController; 
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface; 
use Symfony\Component\HttpFoundation\RedirectResponse; 
use Symfony\Component\Security\Core\Exception\AccessDeniedException; 

class CRUDController extends BaseController 
{ 
    /** 
    * @param ProxyQueryInterface $selectedModelQuery 
    * @param Request    $request 
    * 
    * @return RedirectResponse 
    */ 
    public function batchActionMerge(ProxyQueryInterface $selectedModelQuery) 
    { 
     if ($this->admin->isGranted('EDIT') === false || $this->admin->isGranted('DELETE') === false) 
     { 
      throw new AccessDeniedException(); 
     } 

     $request = $this->get('request'); 
     $modelManager = $this->admin->getModelManager(); 

     $target = $modelManager->find($this->admin->getClass(), $request->get('targetId')); 

     if($target === null){ 
      $this->get('session')->setFlash('sonata_flash_info', 'flash_batch_merge_no_target'); 

      return new RedirectResponse($this->admin->generateUrl('list', array('filter' => $this->admin->getFilterParameters()))); 
     } 

     $selectedModels = $selectedModelQuery->execute(); 

     // do the merge work here 

     try { 
      foreach ($selectedModels as $selectedModel) { 
       $modelManager->delete($selectedModel); 
      } 

      $modelManager->update($selectedModel); 
     } catch (\Exception $e) { 
      $this->get('session')->setFlash('sonata_flash_error', 'flash_batch_merge_error'); 

      return new RedirectResponse($this->admin->generateUrl('list', array('filter' => $this->admin->getFilterParameters()))); 
     } 

     $this->get('session')->setFlash('sonata_flash_success', 'flash_batch_merge_success'); 

     return new RedirectResponse($this->admin->generateUrl('list', array('filter' => $this->admin->getFilterParameters()))); 
    } 
} 

EDIT

試圖從上多對多字段另一個實體類設置的值。但它說數組必須是實體類型。

CRUDController.php

public function batchActionSend(ProxyQueryInterface $selectedModelQuery) { 
     ... 
     $request = $this->get('request'); 
     $modelManager = $this->admin->getModelManager(); 
     $selectedModels = $selectedModelQuery->execute(); 

     return new RedirectResponse($this->container->get('router')->generate('route_name', array('mails' => $selectedModels))); 
    } 

AdminClass.php

public function getNewInstance() { 
    $instance = parent::getNewInstance(); 
    $mailadds = $this->getRequest()->query->get('mails'); 
    foreach ($mailadds as $mailadd) { 
     $instance->addRecipient($mailadd); 
    } 

    return $instance; 
} 
protected function configureFormFields(FormMapper $formMapper) { 
     $formMapper 
      ->add('recipients', 'sonata_type_model', array('label' => 'Хүлээн авагчид', 'multiple' => true, 'by_reference' => false) 
... 
} 

注:收件人是多對多場。

+0

也添加一些代碼。簡單地說,在你試圖獲得'translation_domain'的數組中,該鍵不存在。在使用之前檢查isset。 – Janno

+0

在哪個頁面發生此錯誤?嘗試將錯誤處理設置爲E_ALL,也許可能會創建一個自定義錯誤處理程序,通過它您可以在發生此問題的位置獲取特定文件行。例如:'error_reporting(E_ALL); set_error_handler(「errorHandler」,E_ALL); 函數errorHandler($ errno,$ errstr,$ err_file,$ err_line,$ err_context){ echo「Error $ errno,errstr - $ errstr,err_file - $ errfile,err_line - $ err_line,context - $ err_context); die (); }' – Janno

+0

我發佈的代碼會打印出所有的錯誤,它可能會讓你更接近 – Janno

回答

2

按我的意見,問題應該通過下面的代碼上線420解決:

$translationDomain = isset($batchActions[$action]['translation_domain']) ? $batchActions[$action]['translation_domain'] : $this->admin->getTranslationDomain(); 

問題發生,因爲你沒有檢查,如果這個鍵值「translation_domain」在數組$ batchActions存在[ $行動。據我所知,三元運算符'?'試圖將該值應用於該變量,如果該值在操作之後沒有被設置,則它嘗試第二個操作符。因此,爲什麼我認爲isset應該解決它。

相關問題