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

时间:2015-07-27 08:32:05

标签: php symfony sonata-admin swiftmailer sonata

因此,在我的UsersAdmin中,如果我确认他的帐户,我想向该用户发送电子邮件。(在我的情况下,制作Enabled = true)。我在configureListFields函数

中执行此操作
/**
     * {@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')
        ;
    }

通过阅读文档我认为我需要使用batchAction函数是吗?所以我做到了:

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;
}

现在我对这个功能有两个不清楚的事情:

  1. 如何获取我想要编辑的当前用户数据?

  2. 我是否朝着正确的方向前进?我是否需要覆盖编辑或其他功能?

  3. 解决方案

    最好的方法是在postUpdate事件中进行登录,这样每次更新对象时它都会启动您想要的功能。

    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 个答案:

答案 0 :(得分:0)

您可以使用Saving hooks.

   public function postUpdate($user)
    {
       //code to check if enabled 
       // code to send email    
    }