创建时的 Symfony4 双实体
Symfony4 double entity on create
我正在创建一个简单的邮件程序..
首先,您需要创建一个 MailTemplate(实体)。
这存在一个主题、mailFrom 和一条消息。
然后创建邮件: 这分两步进行。
首先,您选择要发送到的帐户和您的 MailTemplate。
然后你重定向到我设置主题、消息和发件人的另一条路线,所以我可以调整东西。
当我发送(保存邮件)时。它保存了邮件,但复制了我的 MailTemplate 并将 Mailtemplate 保存到。
所以我得到了 1 个邮件和 2 个模板。
我的模板实体
<?php
/**
* Created by PhpStorm.
* User: david
* Date: 26-6-2018
* Time: 20:13
*/
namespace App\Project\MailBundle\Entity;
use App\Project\BaseBundle\Entity\BaseEntity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Doctrine\ORM\Mapping as ORM;
/**
* Class MailTemplates
* @package App\Project\MailBundle\Entity
*/
/**
* @ORM\Entity(repositoryClass="App\Project\MailBundle\Repository\MailTemplatesRepository")
*/
class MailTemplate extends BaseEntity
{
public function __construct()
{
parent::__construct();
$this->active = true;
$this->bulkmails = new ArrayCollection();
$this->mails = new ArrayCollection();
}
/**
* @var string $template
*
* @ORM\Column(name="template", type="string", length=191, nullable=false)
*
*
*/
private $template;
/**
* @var string $mailFrom
*
* @ORM\Column(type="string", length=191, nullable=false, options={"default": "noreply@nachtpost.be"})
* @Assert\NotBlank()
*
*/
private $mailFrom;
/**
* @var string $mailSubject
*
* @ORM\Column(type="string", length=191, nullable=false)
* @Assert\NotBlank()
*
*/
private $mailSubject;
/**
* @var string $mailMessage
*
* @ORM\Column(type="text", nullable=false)
* @Assert\NotBlank()
*
*/
private $mailMessage;
/**
* @ORM\OneToMany(targetEntity="App\Project\MailBundle\Entity\Mail", mappedBy="mailTemplate", orphanRemoval=true)
*/
private $mails;
/**
* @ORM\OneToMany(targetEntity="App\Project\MailBundle\Entity\BulkMail", mappedBy="mailTemplate", orphanRemoval=true)
*/
private $bulkmails;
/**
* @return string
*/
public function getTemplate(): ? string
{
return $this->template;
}
/**
* @param string $template
*/
public function setTemplate(string $template): void
{
$this->template = $template;
}
/**
* @return mixed
*/
public function getMailFrom()
{
return $this->mailFrom;
}
/**
* @param mixed $mailFrom
*/
public function setMailFrom($mailFrom): void
{
$this->mailFrom = $mailFrom;
}
/**
* @return mixed
*/
public function getMailSubject()
{
return $this->mailSubject;
}
/**
* @param mixed $mailSubject
*/
public function setMailSubject($mailSubject): void
{
$this->mailSubject = $mailSubject;
}
/**
* @return mixed
*/
public function getMailMessage()
{
return $this->mailMessage;
}
/**
* @param mixed $mailMessage
*/
public function setMailMessage($mailMessage): void
{
$this->mailMessage = $mailMessage;
}
/**
* @return mixed
*/
public function getMails(): collection
{
return $this->mails;
}
/**
* @param mixed $mails
*/
public function setMails($mails)
{
$this->mails = $mails;
}
/**
* @return mixed
*/
public function getBulkmails(): collection
{
return $this->bulkmails;
}
/**
* @param mixed $bulkmails
*/
public function setBulkmails($bulkmails)
{
$this->bulkmails = $bulkmails;
}
public function __toString()
{
return $this->getTemplate();
}
}
我的邮件实体
<?php
/**
* Created by PhpStorm.
* User: david
* Date: 26-6-2018
* Time: 20:13
*/
namespace App\Project\MailBundle\Entity;
use App\Project\BaseBundle\Entity\BaseEntity;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;
/**
* Class BulkMail
* @package App\Project\MailBundle\Entity
*/
/**
* @ORM\Entity(repositoryClass="App\Project\MailBundle\Repository\BulkMailRepository")
*/
class BulkMail extends BaseEntity
{
public function __construct()
{
parent::__construct();
$this->mailTo = new ArrayCollection();
$this->active = true;
}
/**
* @ORM\ManyToOne(targetEntity="App\Project\MailBundle\Entity\MailTemplate", inversedBy="mails", cascade={"persist", "remove"})
* @ORM\JoinColumn(name="bulkMail_id", referencedColumnName="id", nullable=false)
*/
private $mailTemplate;
/**
* @ORM\ManyToMany(targetEntity="App\Project\AccountBundle\Entity\Account")
* @ORM\JoinColumn(name="account_id", referencedColumnName="id", nullable=false)
* @Assert\NotBlank()
*/
private $mailTo;
/**
* @var string $mailFrom
*
* @ORM\Column(type="string", length=191, nullable=true)
*
*/
private $mailFrom;
/**
* @var string $mailSubject
*
* @ORM\Column(type="string", length=191, nullable=true)
*
*/
private $mailSubject;
/**
* @var string $mailMessage
*
* @ORM\Column(type="text", nullable=true)
*
*/
private $mailMessage;
/**
* @return mixed
*/
public function getMailTo()
{
return $this->mailTo;
}
/**
* @param mixed $mailTo
*/
public function setMailTo($mailTo): void
{
$this->mailTo = $mailTo;
}
/**
* @return mixed
*/
public function getMailFrom()
{
return $this->mailFrom;
}
/**
* @param mixed $mailFrom
*/
public function setMailFrom($mailFrom): void
{
$this->mailFrom = $mailFrom;
}
/**
* @return mixed
*/
public function getMailSubject()
{
return $this->mailSubject;
}
/**
* @param mixed $mailSubject
*/
public function setMailSubject($mailSubject): void
{
$this->mailSubject = $mailSubject;
}
/**
* @return mixed
*/
public function getMailMessage()
{
return $this->mailMessage;
}
/**
* @param mixed $mailMessage
*/
public function setMailMessage($mailMessage): void
{
$this->mailMessage = $mailMessage;
}
/**
* @param mixed $accountId
*/
public function setMail($account): void
{
$this->account = $account;
}
/**
* @return mixed
*/
public function getMailTemplate()
{
return $this->mailTemplate;
}
/**
* @param mixed $mailTemplate
*/
public function setMailTemplate($mailTemplate)
{
$this->mailTemplate = $mailTemplate;
}
public function __toString()
{
return $this->mailSubject;
}
}
我的控制器
<?php
/**
* Created by PhpStorm.
* User: david
* Date: 5-7-2018
* Time: 22:41
*/
namespace App\Project\MailBundle\Controller;
use App\Project\AccountBundle\Entity\Account;
use App\Project\MailBundle\Entity\BulkMail;
use App\Project\MailBundle\Entity\MailTemplate;
use App\Project\MailBundle\Forms\BulkAddMailAdminType;
use App\Project\MailBundle\Forms\bulkSelectmailAdminType;
use App\Project\BaseBundle\Controller\BaseController;
use Symfony\Component\HttpFoundation\Request;
class BulkMailController extends BaseController
{
public function BulkMailIndexAction()
{
$MailRepository = $this->getDoctrine()->getRepository(BulkMail::class);
$items = $MailRepository->findAll();
return $this->render('@ProjectMail/mails/bulk/index.html.twig', array(
'items' => $items
));
}
public function bulkSelectAction(Request $request) {
$mail = new BulkMail();
$form = $this->createForm(BulkSelectmailAdminType::class, $mail);
if ($request->getMethod() == 'POST') {
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$TemplateRepository = $this->getDoctrine()->getRepository(MailTemplate::class);
$tId = $mail->getMailTemplate()->getId();
$template = $TemplateRepository->findOneBy(array('id' => $tId));
$mail->setMailSubject($template->getMailSubject());
$mail->setMailFrom($template->getMailFrom());
$mail->setMailMessage($template->getMailMessage());
$mail->setMailTemplate($template);
$this->container->get('session')->set('Bmail', $mail);
$response = $this->redirectToRoute('mailBulkSend');
return $response;
}
}
$label = "Email opmaken";
$response = $this->render('@ProjectMail/mails/bulk/addBulk_select.html.twig', array(
'form' => $form->createView(),
'label' => $label
));
return $response;
}
/**
* @param Request $request
* @param \Swift_Mailer $mailer
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
*/
public function bulkMailAction(Request $request, \Swift_Mailer $mailer) {
$Bmail = $this->container->get('session')->get('Bmail');
if ($Bmail) {
$accounts = array();
$AccountRepository = $this->getDoctrine()->getRepository(Account::class);
$count = 0;
foreach ($Bmail->getMailTo() as $key => $account) {
$accounts[] = $AccountRepository->findOneById($account->getId());
$count ++;
}
$form = $this->createForm(BulkAddMailAdminType::class, $Bmail);
if ($request->getMethod() == 'POST') {
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$Bmail->setMailTo($accounts);
//dump($Bmail->getMailTemplate()); die;
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($Bmail);
$entityManager->flush();
// Get account emails in array
foreach($accounts as $key => $acc){
$persons[$acc->getEmail()] = $acc->getEmail();
}
$message = (new \Swift_Message($Bmail->getMailSubject()))
->setContentType("text/html")
->setFrom($Bmail->getMailFrom())
->setTo($persons)
->setBody(
$this->renderView(
'@ProjectTemplate/_templates/base_mail.html.twig', array(
'type' => 'emailDefault',
'template' => $Bmail
)
)
)
;
// $mailer->send($message);
$response = $this->redirectToRoute('mailsBulkList');
return $response;
}
}
$label = "Email verzenden";
$response = $this->render('@ProjectMail/mails/bulk/addMail.html.twig', array(
'form' => $form->createView(),
'mail' => $Bmail,
'accounts' => $accounts,
'label' => $label
));
} else {
$response = $this->redirectToRoute('mailBulkAdd');
}
return $response;
}
public function deleteAction(BulkMail $mail)
{
$em = $this->getDoctrine()->getManager();
$em->remove($mail);
$em->flush();
return $this->redirectToRoute('mailsBulkList');
}
}
我不知道为什么会这样,也不知道该如何处理。
建议?提前!
感谢大家的帮助,非常感谢!
经过一些调试我得出了这个..
不确定它是否应该是这样,但它有效..
我在第一个操作中保存我的邮件并在我的会话中传递 id,在第二个操作中我通过表单传递邮件,(如果需要可以进行更改)并发送(再次保存)。
做了一些清理 :)
<?php
/**
* Created by PhpStorm.
* User: david
* Date: 5-7-2018
* Time: 22:41
*/
namespace App\Project\MailBundle\Controller;
use App\Project\AccountBundle\Entity\Account;
use App\Project\MailBundle\Entity\BulkMail;
use App\Project\MailBundle\Entity\MailTemplate;
use App\Project\MailBundle\Forms\BulkAddMailAdminType;
use App\Project\MailBundle\Forms\bulkSelectmailAdminType;
use App\Project\BaseBundle\Controller\BaseController;
use Symfony\Component\HttpFoundation\Request;
class BulkMailController extends BaseController
{
public function BulkMailIndexAction()
{
$MailRepository = $this->getDoctrine()->getRepository(BulkMail::class);
$items = $MailRepository->findAll();
return $this->render('@ProjectMail/mails/bulk/index.html.twig', array(
'items' => $items
));
}
public function bulkSelectAction(Request $request) {
$mail = new BulkMail();
$form = $this->createForm(BulkSelectmailAdminType::class, $mail);
if ($request->getMethod() == 'POST') {
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$template = $mail->getMailTemplate();
$mail->setMailSubject($template->getMailSubject());
$mail->setMailFrom($template->getMailFrom());
$mail->setMailMessage($template->getMailMessage());
$entityManager->persist($mail);
$entityManager->flush();
$this->container->get('session')->set('Bmail', $mail->getId());
$response = $this->redirectToRoute('mailBulkSend');
return $response;
}
}
$label = "Email opmaken";
$response = $this->render('@ProjectMail/mails/bulk/addBulk_select.html.twig', array(
'form' => $form->createView(),
'label' => $label
));
return $response;
}
/**
* @param Request $request
* @param \Swift_Mailer $mailer
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
*/
public function bulkMailAction(Request $request, \Swift_Mailer $mailer) {
$BmailId = $this->container->get('session')->get('Bmail');
if ($BmailId) {
$entityManager = $this->getDoctrine()->getManager();
$mailRepo = $this->getDoctrine()->getRepository(BulkMail::class);
$Bmail = $mailRepo->find($BmailId);
$accounts = array();
$AccountRepository = $this->getDoctrine()->getRepository(Account::class);
$count = 0;
foreach ($Bmail->getMailTo() as $key => $account) {
$accounts[] = $AccountRepository->findOneById($account->getId());
$count ++;
}
$form = $this->createForm(BulkAddMailAdminType::class, $Bmail);
if ($request->getMethod() == 'POST') {
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$Bmail->setMailTo($accounts);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($Bmail);
$entityManager->flush();
// Get account emails in array
foreach($accounts as $key => $acc){
$persons[$acc->getEmail()] = $acc->getEmail();
}
$message = (new \Swift_Message($Bmail->getMailSubject()))
->setContentType("text/html")
->setFrom($Bmail->getMailFrom())
->setTo($persons)
->setBody(
$this->renderView(
'@ProjectTemplate/_templates/base_mail.html.twig', array(
'type' => 'emailDefault',
'template' => $Bmail
)
)
)
;
// $mailer->send($message);
$response = $this->redirectToRoute('mailsBulkList');
return $response;
}
}
$label = "Email verzenden";
$response = $this->render('@ProjectMail/mails/bulk/addMail.html.twig', array(
'form' => $form->createView(),
'mail' => $Bmail,
'accounts' => $accounts,
'label' => $label
));
} else {
$response = $this->redirectToRoute('mailBulkAdd');
}
return $response;
}
public function deleteAction(BulkMail $mail)
{
$em = $this->getDoctrine()->getManager();
$em->remove($mail);
$em->flush();
return $this->redirectToRoute('mailsBulkList');
}
}
我正在创建一个简单的邮件程序.. 首先,您需要创建一个 MailTemplate(实体)。 这存在一个主题、mailFrom 和一条消息。 然后创建邮件: 这分两步进行。 首先,您选择要发送到的帐户和您的 MailTemplate。 然后你重定向到我设置主题、消息和发件人的另一条路线,所以我可以调整东西。
当我发送(保存邮件)时。它保存了邮件,但复制了我的 MailTemplate 并将 Mailtemplate 保存到。 所以我得到了 1 个邮件和 2 个模板。
我的模板实体
<?php
/**
* Created by PhpStorm.
* User: david
* Date: 26-6-2018
* Time: 20:13
*/
namespace App\Project\MailBundle\Entity;
use App\Project\BaseBundle\Entity\BaseEntity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Doctrine\ORM\Mapping as ORM;
/**
* Class MailTemplates
* @package App\Project\MailBundle\Entity
*/
/**
* @ORM\Entity(repositoryClass="App\Project\MailBundle\Repository\MailTemplatesRepository")
*/
class MailTemplate extends BaseEntity
{
public function __construct()
{
parent::__construct();
$this->active = true;
$this->bulkmails = new ArrayCollection();
$this->mails = new ArrayCollection();
}
/**
* @var string $template
*
* @ORM\Column(name="template", type="string", length=191, nullable=false)
*
*
*/
private $template;
/**
* @var string $mailFrom
*
* @ORM\Column(type="string", length=191, nullable=false, options={"default": "noreply@nachtpost.be"})
* @Assert\NotBlank()
*
*/
private $mailFrom;
/**
* @var string $mailSubject
*
* @ORM\Column(type="string", length=191, nullable=false)
* @Assert\NotBlank()
*
*/
private $mailSubject;
/**
* @var string $mailMessage
*
* @ORM\Column(type="text", nullable=false)
* @Assert\NotBlank()
*
*/
private $mailMessage;
/**
* @ORM\OneToMany(targetEntity="App\Project\MailBundle\Entity\Mail", mappedBy="mailTemplate", orphanRemoval=true)
*/
private $mails;
/**
* @ORM\OneToMany(targetEntity="App\Project\MailBundle\Entity\BulkMail", mappedBy="mailTemplate", orphanRemoval=true)
*/
private $bulkmails;
/**
* @return string
*/
public function getTemplate(): ? string
{
return $this->template;
}
/**
* @param string $template
*/
public function setTemplate(string $template): void
{
$this->template = $template;
}
/**
* @return mixed
*/
public function getMailFrom()
{
return $this->mailFrom;
}
/**
* @param mixed $mailFrom
*/
public function setMailFrom($mailFrom): void
{
$this->mailFrom = $mailFrom;
}
/**
* @return mixed
*/
public function getMailSubject()
{
return $this->mailSubject;
}
/**
* @param mixed $mailSubject
*/
public function setMailSubject($mailSubject): void
{
$this->mailSubject = $mailSubject;
}
/**
* @return mixed
*/
public function getMailMessage()
{
return $this->mailMessage;
}
/**
* @param mixed $mailMessage
*/
public function setMailMessage($mailMessage): void
{
$this->mailMessage = $mailMessage;
}
/**
* @return mixed
*/
public function getMails(): collection
{
return $this->mails;
}
/**
* @param mixed $mails
*/
public function setMails($mails)
{
$this->mails = $mails;
}
/**
* @return mixed
*/
public function getBulkmails(): collection
{
return $this->bulkmails;
}
/**
* @param mixed $bulkmails
*/
public function setBulkmails($bulkmails)
{
$this->bulkmails = $bulkmails;
}
public function __toString()
{
return $this->getTemplate();
}
}
我的邮件实体
<?php
/**
* Created by PhpStorm.
* User: david
* Date: 26-6-2018
* Time: 20:13
*/
namespace App\Project\MailBundle\Entity;
use App\Project\BaseBundle\Entity\BaseEntity;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;
/**
* Class BulkMail
* @package App\Project\MailBundle\Entity
*/
/**
* @ORM\Entity(repositoryClass="App\Project\MailBundle\Repository\BulkMailRepository")
*/
class BulkMail extends BaseEntity
{
public function __construct()
{
parent::__construct();
$this->mailTo = new ArrayCollection();
$this->active = true;
}
/**
* @ORM\ManyToOne(targetEntity="App\Project\MailBundle\Entity\MailTemplate", inversedBy="mails", cascade={"persist", "remove"})
* @ORM\JoinColumn(name="bulkMail_id", referencedColumnName="id", nullable=false)
*/
private $mailTemplate;
/**
* @ORM\ManyToMany(targetEntity="App\Project\AccountBundle\Entity\Account")
* @ORM\JoinColumn(name="account_id", referencedColumnName="id", nullable=false)
* @Assert\NotBlank()
*/
private $mailTo;
/**
* @var string $mailFrom
*
* @ORM\Column(type="string", length=191, nullable=true)
*
*/
private $mailFrom;
/**
* @var string $mailSubject
*
* @ORM\Column(type="string", length=191, nullable=true)
*
*/
private $mailSubject;
/**
* @var string $mailMessage
*
* @ORM\Column(type="text", nullable=true)
*
*/
private $mailMessage;
/**
* @return mixed
*/
public function getMailTo()
{
return $this->mailTo;
}
/**
* @param mixed $mailTo
*/
public function setMailTo($mailTo): void
{
$this->mailTo = $mailTo;
}
/**
* @return mixed
*/
public function getMailFrom()
{
return $this->mailFrom;
}
/**
* @param mixed $mailFrom
*/
public function setMailFrom($mailFrom): void
{
$this->mailFrom = $mailFrom;
}
/**
* @return mixed
*/
public function getMailSubject()
{
return $this->mailSubject;
}
/**
* @param mixed $mailSubject
*/
public function setMailSubject($mailSubject): void
{
$this->mailSubject = $mailSubject;
}
/**
* @return mixed
*/
public function getMailMessage()
{
return $this->mailMessage;
}
/**
* @param mixed $mailMessage
*/
public function setMailMessage($mailMessage): void
{
$this->mailMessage = $mailMessage;
}
/**
* @param mixed $accountId
*/
public function setMail($account): void
{
$this->account = $account;
}
/**
* @return mixed
*/
public function getMailTemplate()
{
return $this->mailTemplate;
}
/**
* @param mixed $mailTemplate
*/
public function setMailTemplate($mailTemplate)
{
$this->mailTemplate = $mailTemplate;
}
public function __toString()
{
return $this->mailSubject;
}
}
我的控制器
<?php
/**
* Created by PhpStorm.
* User: david
* Date: 5-7-2018
* Time: 22:41
*/
namespace App\Project\MailBundle\Controller;
use App\Project\AccountBundle\Entity\Account;
use App\Project\MailBundle\Entity\BulkMail;
use App\Project\MailBundle\Entity\MailTemplate;
use App\Project\MailBundle\Forms\BulkAddMailAdminType;
use App\Project\MailBundle\Forms\bulkSelectmailAdminType;
use App\Project\BaseBundle\Controller\BaseController;
use Symfony\Component\HttpFoundation\Request;
class BulkMailController extends BaseController
{
public function BulkMailIndexAction()
{
$MailRepository = $this->getDoctrine()->getRepository(BulkMail::class);
$items = $MailRepository->findAll();
return $this->render('@ProjectMail/mails/bulk/index.html.twig', array(
'items' => $items
));
}
public function bulkSelectAction(Request $request) {
$mail = new BulkMail();
$form = $this->createForm(BulkSelectmailAdminType::class, $mail);
if ($request->getMethod() == 'POST') {
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$TemplateRepository = $this->getDoctrine()->getRepository(MailTemplate::class);
$tId = $mail->getMailTemplate()->getId();
$template = $TemplateRepository->findOneBy(array('id' => $tId));
$mail->setMailSubject($template->getMailSubject());
$mail->setMailFrom($template->getMailFrom());
$mail->setMailMessage($template->getMailMessage());
$mail->setMailTemplate($template);
$this->container->get('session')->set('Bmail', $mail);
$response = $this->redirectToRoute('mailBulkSend');
return $response;
}
}
$label = "Email opmaken";
$response = $this->render('@ProjectMail/mails/bulk/addBulk_select.html.twig', array(
'form' => $form->createView(),
'label' => $label
));
return $response;
}
/**
* @param Request $request
* @param \Swift_Mailer $mailer
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
*/
public function bulkMailAction(Request $request, \Swift_Mailer $mailer) {
$Bmail = $this->container->get('session')->get('Bmail');
if ($Bmail) {
$accounts = array();
$AccountRepository = $this->getDoctrine()->getRepository(Account::class);
$count = 0;
foreach ($Bmail->getMailTo() as $key => $account) {
$accounts[] = $AccountRepository->findOneById($account->getId());
$count ++;
}
$form = $this->createForm(BulkAddMailAdminType::class, $Bmail);
if ($request->getMethod() == 'POST') {
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$Bmail->setMailTo($accounts);
//dump($Bmail->getMailTemplate()); die;
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($Bmail);
$entityManager->flush();
// Get account emails in array
foreach($accounts as $key => $acc){
$persons[$acc->getEmail()] = $acc->getEmail();
}
$message = (new \Swift_Message($Bmail->getMailSubject()))
->setContentType("text/html")
->setFrom($Bmail->getMailFrom())
->setTo($persons)
->setBody(
$this->renderView(
'@ProjectTemplate/_templates/base_mail.html.twig', array(
'type' => 'emailDefault',
'template' => $Bmail
)
)
)
;
// $mailer->send($message);
$response = $this->redirectToRoute('mailsBulkList');
return $response;
}
}
$label = "Email verzenden";
$response = $this->render('@ProjectMail/mails/bulk/addMail.html.twig', array(
'form' => $form->createView(),
'mail' => $Bmail,
'accounts' => $accounts,
'label' => $label
));
} else {
$response = $this->redirectToRoute('mailBulkAdd');
}
return $response;
}
public function deleteAction(BulkMail $mail)
{
$em = $this->getDoctrine()->getManager();
$em->remove($mail);
$em->flush();
return $this->redirectToRoute('mailsBulkList');
}
}
我不知道为什么会这样,也不知道该如何处理。 建议?提前!
感谢大家的帮助,非常感谢!
经过一些调试我得出了这个.. 不确定它是否应该是这样,但它有效.. 我在第一个操作中保存我的邮件并在我的会话中传递 id,在第二个操作中我通过表单传递邮件,(如果需要可以进行更改)并发送(再次保存)。
做了一些清理 :)
<?php
/**
* Created by PhpStorm.
* User: david
* Date: 5-7-2018
* Time: 22:41
*/
namespace App\Project\MailBundle\Controller;
use App\Project\AccountBundle\Entity\Account;
use App\Project\MailBundle\Entity\BulkMail;
use App\Project\MailBundle\Entity\MailTemplate;
use App\Project\MailBundle\Forms\BulkAddMailAdminType;
use App\Project\MailBundle\Forms\bulkSelectmailAdminType;
use App\Project\BaseBundle\Controller\BaseController;
use Symfony\Component\HttpFoundation\Request;
class BulkMailController extends BaseController
{
public function BulkMailIndexAction()
{
$MailRepository = $this->getDoctrine()->getRepository(BulkMail::class);
$items = $MailRepository->findAll();
return $this->render('@ProjectMail/mails/bulk/index.html.twig', array(
'items' => $items
));
}
public function bulkSelectAction(Request $request) {
$mail = new BulkMail();
$form = $this->createForm(BulkSelectmailAdminType::class, $mail);
if ($request->getMethod() == 'POST') {
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$template = $mail->getMailTemplate();
$mail->setMailSubject($template->getMailSubject());
$mail->setMailFrom($template->getMailFrom());
$mail->setMailMessage($template->getMailMessage());
$entityManager->persist($mail);
$entityManager->flush();
$this->container->get('session')->set('Bmail', $mail->getId());
$response = $this->redirectToRoute('mailBulkSend');
return $response;
}
}
$label = "Email opmaken";
$response = $this->render('@ProjectMail/mails/bulk/addBulk_select.html.twig', array(
'form' => $form->createView(),
'label' => $label
));
return $response;
}
/**
* @param Request $request
* @param \Swift_Mailer $mailer
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
*/
public function bulkMailAction(Request $request, \Swift_Mailer $mailer) {
$BmailId = $this->container->get('session')->get('Bmail');
if ($BmailId) {
$entityManager = $this->getDoctrine()->getManager();
$mailRepo = $this->getDoctrine()->getRepository(BulkMail::class);
$Bmail = $mailRepo->find($BmailId);
$accounts = array();
$AccountRepository = $this->getDoctrine()->getRepository(Account::class);
$count = 0;
foreach ($Bmail->getMailTo() as $key => $account) {
$accounts[] = $AccountRepository->findOneById($account->getId());
$count ++;
}
$form = $this->createForm(BulkAddMailAdminType::class, $Bmail);
if ($request->getMethod() == 'POST') {
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$Bmail->setMailTo($accounts);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($Bmail);
$entityManager->flush();
// Get account emails in array
foreach($accounts as $key => $acc){
$persons[$acc->getEmail()] = $acc->getEmail();
}
$message = (new \Swift_Message($Bmail->getMailSubject()))
->setContentType("text/html")
->setFrom($Bmail->getMailFrom())
->setTo($persons)
->setBody(
$this->renderView(
'@ProjectTemplate/_templates/base_mail.html.twig', array(
'type' => 'emailDefault',
'template' => $Bmail
)
)
)
;
// $mailer->send($message);
$response = $this->redirectToRoute('mailsBulkList');
return $response;
}
}
$label = "Email verzenden";
$response = $this->render('@ProjectMail/mails/bulk/addMail.html.twig', array(
'form' => $form->createView(),
'mail' => $Bmail,
'accounts' => $accounts,
'label' => $label
));
} else {
$response = $this->redirectToRoute('mailBulkAdd');
}
return $response;
}
public function deleteAction(BulkMail $mail)
{
$em = $this->getDoctrine()->getManager();
$em->remove($mail);
$em->flush();
return $this->redirectToRoute('mailsBulkList');
}
}