drvxnivoqf17568697 2015-07-27 08:32
浏览 15
已采纳

在SonataAdminBundle上发送编辑的电子邮件

So in my UsersAdmin I want to send an email to that user if I confirm his account.(in my case, making Enabled = true). I do this in the configureListFields function

/**
     * {@inheritdoc}
     */
    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper
            ->addIdentifier('username')
            ->add('email')
            ->add('groups')
            ->add('enabled', null, array('editable' => true)) //here
            ->add('locked', null, array('editable' => true))
            ->add('createdAt')
        ;
    }

By reading the documentation I think i need to use the batchAction function yes? So I made this:

public function getBatchActions()
{
    // retrieve the default batch actions (currently only delete)
    $actions = parent::getBatchActions();
    $container = $this->getConfigurationPool()->getContainer();
    $user = //how to get the user that i am editing right now?

    if ($this->hasRoute('edit') && $this->isGranted('EDIT')) {
        $body = $container->get('templating')->render('MpShopBundle:Registration:registrationEmail.html.twig', array('user'=> $user));

        $message = Swift_message::newInstance();
        $message->setSubject($container->get('translator')->trans('registration.successful'))
            ->setFrom($container->getParameter('customer.care.email.sender'))
            ->setTo('email@contact.lt')
            ->setBody($body, 'text/html');
        $container->get('mailer')->send($message);

    }

    return $actions;
}

Now I am stuck with two unclear thing with this function:

  1. How can I get the current user data hat I want to edit?

  2. Am I even going in the right direction? Do I need to override edit or maybe some other function?

THE SOLUTION

The best way is to do your login in the postUpdate event, so that everytime you update an object it initiates the functions you want.

public function postUpdate($user)
{
    if($user->getEnabled() == true) {

        $container = $this->getConfigurationPool()->getContainer();

        $body = $container->get('templating')->render('MpShopBundle:Registration:registrationEmail.html.twig', array('user' => $user));

        $message = Swift_message::newInstance();
        $message->setSubject($container->get('translator')->trans('registration.successful'))
            ->setFrom($container->getParameter('customer.care.email.sender'))
            ->setTo('email@contact.lt')
            ->setBody($body, 'text/html');
        $container->get('mailer')->send($message);
    }
}
  • 写回答

1条回答 默认 最新

  • douxie3625 2015-07-27 09:48
    关注

    you can use Saving hooks.

       public function postUpdate($user)
        {
           //code to check if enabled 
           // code to send email    
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探