Sonata admin/swiftmailer 如何从列表中取回信息
Sonata admin/swiftmailer how to take back information from the list
我目前正在尝试在我的项目中使用 swiftmailer。我目前正在研究 Sonata Admin,我想知道如何检索列表中显示的对象,以便能够检索关联的邮件地址,从而向该列表中包含的所有地址发送电子邮件。我想浏览 sonata 显示的列表,因为他们的过滤系统运行良好,我会用它来选择我想向其发送电子邮件的人。我在 symfony 文档中看到可以将邮件发送到地址 table 这种形式:
$to = array('one@example.com', 'two@example.com', 'three@example.com');
$message = (new \Swift_Message('Hello Email'))
->setFrom('send@example.com')
->setTo(array($to))
->setBody('html content goes here', 'text/html');
$mailer->send($message);
但我不知道如何从列表中取回对象。
来自这个网格。
你能帮我吗谢谢?
Ps :
我只是想在列表下方放一个按钮,向列表中显示的所有人发送电子邮件。
非常感谢。
编辑:
我仍在搜索,我发现 sql 请求类似于 't0.id' 和 'c0.id'。 t0 和 c0 是对象的名称?总是这样吗? t0 和 c0 有什么区别?
您可以通过向管理员列表添加操作来完成此操作。
为此,首先在 YourAdminBundle\Controller
文件夹中创建一个新的 class,扩展 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
,进入您的 class 管理块,并通过引用完成 arguments
属性 的第三个参数您的自定义 CRUD 控制器:
arguments: [null, YourBundle\Entity\YourEntity,YourAdminBundle:CustomCRUD]
最后,要允许您使用自定义操作,请转到您的管理员 Class 并添加此功能:
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" 复选框旁边。
我目前正在尝试在我的项目中使用 swiftmailer。我目前正在研究 Sonata Admin,我想知道如何检索列表中显示的对象,以便能够检索关联的邮件地址,从而向该列表中包含的所有地址发送电子邮件。我想浏览 sonata 显示的列表,因为他们的过滤系统运行良好,我会用它来选择我想向其发送电子邮件的人。我在 symfony 文档中看到可以将邮件发送到地址 table 这种形式:
$to = array('one@example.com', 'two@example.com', 'three@example.com');
$message = (new \Swift_Message('Hello Email'))
->setFrom('send@example.com')
->setTo(array($to))
->setBody('html content goes here', 'text/html');
$mailer->send($message);
但我不知道如何从列表中取回对象。 来自这个网格。
Ps : 我只是想在列表下方放一个按钮,向列表中显示的所有人发送电子邮件。
非常感谢。
编辑: 我仍在搜索,我发现 sql 请求类似于 't0.id' 和 'c0.id'。 t0 和 c0 是对象的名称?总是这样吗? t0 和 c0 有什么区别?
您可以通过向管理员列表添加操作来完成此操作。
为此,首先在 YourAdminBundle\Controller
文件夹中创建一个新的 class,扩展 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
,进入您的 class 管理块,并通过引用完成 arguments
属性 的第三个参数您的自定义 CRUD 控制器:
arguments: [null, YourBundle\Entity\YourEntity,YourAdminBundle:CustomCRUD]
最后,要允许您使用自定义操作,请转到您的管理员 Class 并添加此功能:
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" 复选框旁边。