2017-07-07 51 views
1

我目前正在尝试在我的项目中使用swiftmailer。我目前正在使用Sonata Admin,我想知道如何检索列表中显示的对象,以便能够检索关联的邮件地址,从而向包含在此列表中的所有地址发送电子邮件。我想通过奏鸣曲显示的列表,因为他们的过滤系统工作得很好,我会用它来选择我想发送电子邮件的人。我symfony的文件上看到,有可能将邮件发送到一个地址表形式如下:索纳塔管理员/ swiftmailer如何从列表中取回信息

$to = array('[email protected]', '[email protected]', '[email protected]'); 

$message = (new \Swift_Message('Hello Email')) 
    ->setFrom('[email protected]') 
      ->setTo(array($to)) 
      ->setBody('html content goes here', 'text/html'); 

$mailer->send($message); 

但我不知道如何夺回对象形成的列表。 从这个网格。

grid 你能帮我谢谢吗?

Ps: 我只是想把一个按钮放在列表中,向列表中显示的所有人发送电子邮件。

非常感谢。

编辑: 我仍在搜索,我发现SQL请求像't0.id'和'c0.id'。 t0和c0是对象的名称?它总是那样吗? t0和c0有什么区别?

回答

1

您可以通过向管理员列表中添加一项操作来完成此操作。
为此,请先在YourAdminBundle\Controller文件夹中创建一个新类,并将其扩展为Sonata\AdminBundle\Controller\CRUDController

您的自定义操作可能是这样的,例如:

/** @property YourAdminClass $admin */ 
     public function batchActionSendMail(ProxyQueryInterface $selectedModelQuery ,$type = 'sendMails') { 

     if (false === $this->admin->isGranted('EDIT')) { 
      throw new AccessDeniedException(); 
     } 

      /* selected objects in your list !! */ 
      $selectedModels = $selectedModelQuery->execute(); 

      try{ 
      foreach ($selectedModels as $selectedModel){ 
       // your code to retrieve objects mails here (for instance) 
       } 
       //code to send your mails 

       } 
       catch(\Exception $e) 
       { 
        $this->addFlash('sonata_flash_error', "error"); 
       } 
       $this->addFlash('sonata_flash_success', 'mails sent') 

     return new RedirectResponse($this->admin->generateUrl('list')); 
      } 

为了让这个自定义的CRUD控制器活动,去services.yml,让你的类管理模块,并通过引用完成arguments财产的第三PARAM您的自定义CRUD控制器:

arguments: [null, YourBundle\Entity\YourEntity,YourAdminBundle:CustomCRUD] 

最后,为了让您使用您的自定义操作,转到您的管理类,并添加此功能:

public function getBatchActions() 
    { 
     if ($this->hasRoute('edit')) { 

       $actions['sendMails'] = array(
        'label'   => $this->trans('batch.sendMails.action'), 
        'ask_confirmation' => true, // by default always true 
       ); 
      } 
     return $actions; 
    } 

该操作将位于管理员列表底部的下拉列表中“Select all”复选框旁边。

相关问题