我正在尝试在 Sonata 管理员上添加新的批处理操作。但是当我在 Sonata-Project 上实现示例时对于我的项目,它说:
Notice: Undefined index: 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())));
}
}
编辑
尝试在 ManyToMany 字段上设置另一个实体类的值。但它说数组必须是实体类型。
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)
...
}
Note: recipients is ManyToMany field.
最佳答案
根据我的评论,问题应该通过第 420 行的以下代码解决:
$translationDomain = isset($batchActions[$action]['translation_domain']) ? $batchActions[$action]['translation_domain'] : $this->admin->getTranslationDomain();
发生问题是因为您没有检查数组 $batchActions[$action] 中是否存在键“translation_domain”。据我所知,三元运算符“?”尝试将值应用于变量,如果操作后未设置该值,则会尝试第二个运算符。因此,为什么我认为 isset 应该修复它。
关于php - 在 sonata admin 上创建批处理操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38692359/