Symfony变量未传递给Twig资源

时间:2015-02-10 21:03:15

标签: php forms email symfony twig

我继承了一个PHP Symfony2应用程序。在应用程序中,我有一个表单,在提交时假设发送一封电子邮件,但其中一个变量未正确传递给电子邮件。 (即使从电子邮件模板中取出所有变量,仍然无法处理提交)

我知道要查看这些代码很多,所以也许你可能提供一个好方法来至少开始解决这个问题......我对Symfony来说还是一个新手。

我收到了以下电子邮件错误(statusChange.txt.twig)

Impossible to access an attribute ("name") on a NULL variable ("") in MainBundle:Email:statusChange.txt.twig at line 1

这是第一个显示表单

的控制器
/**
 * @Route("/manage/site/compliance/{id}")
 * @Template()
 */
public function complianceAction($id)
{
    $user = $this->getUser();
    $em = $this->getDoctrine()->getManager();

    $qb = $em->createQueryBuilder();
    $qb->select(array('site', 'compliance', 'criterion', 'category'))
        ->from('MainBundle\Entity\Site', 'site')
        ->join('site.compliance', 'compliance')
        ->join('compliance.criterion', 'criterion')
        ->join('criterion.category', 'category')
        ->where('site.id = :site_id')
        ->andWhere('site.active = true');

    $qb->setParameter('site_id', $id);
    $site = $qb->getQuery()->getResult();

    if (!$site) {
        throw new NotFoundHttpException('Site not found.');
    }
    $site = array_shift($site);

    if (!$this->get('security.context')->isGranted('ROLE_ADMIN')) {
        $this->get('session')->getFlashBag()->add(
            'permissions_error',
            'You do not have permission to review the ‘'.$site->getTitle().'’ site.'
        );
        return $this->redirect($this->generateUrl('manage_index'));
    }

    $statuses = $em->getRepository('MainBundle:Status')
                   ->findAll();

    $reviewStatusManager = $this->get('ReviewStatusManager');
    $nextReviewStatus = $reviewStatusManager->getNextReviewStatus($site, $user);

    $form = $this->createForm(new SiteComplianceType(), $site, array(
        'em' => $em,
    ));

    return array(
        'user' => $user,
        'site' => $site,
        'statuses' => $statuses,
        'form' => $form->createView(),
        'nextReviewStatus' => $nextReviewStatus,
    );
}

这是第二个控制器,它是ProcessAction

/**
 * @Route("/manage/site/compliance/{id}")
 * @Template()
 */
public function complianceProcessAction($id)
{
    $user = $this->getUser();
    $em = $this->getDoctrine()->getManager();

    $qb = $em->createQueryBuilder();
    $qb->select(array('site', 'compliance', 'criterion', 'category'))
        ->from('MainBundle\Entity\Site', 'site')
        ->join('site.compliance', 'compliance')
        ->join('compliance.criterion', 'criterion')
        ->join('criterion.category', 'category')
        ->where('site.id = :site_id');
    $qb->setParameter('site_id', $id);
    $site = $qb->getQuery()->getResult();
    $site = array_shift($site);

    $form = $this->createForm(new SiteComplianceType(), $site, array(
        'em' => $em,
    ));
    $form->handleRequest($this->getRequest());

    if ($form->isValid()) {
        $reviewStatus = $this->get('ReviewStatusManager')->getNextReviewStatus($site, $user);
        $noChangeReviewStatus = ($reviewStatus->getMessage() === $site->getReviewStatus()->getMessage());
        if (!$noChangeReviewStatus) {
            $site->setReviewStatus($reviewStatus);
        }
        $statusManager = $this->get('StatusManager');
        $newStatus = $statusManager->getNextStatus($site);
        $site->setStatus($newStatus);

        $mailer = $this->get('EmailDispatcher');
        if ($reviewStatus->getMessage() == 'Review complete') {
            $mailer->sendStatusChangeEmail($site);
        }
        elseif(strpos($reviewStatus->getMessage(), 'Awaiting review') === 0 && !$noChangeReviewStatus) {
            $mailer->sendAwaitingReviewEmail($site);
        }

        $action = new Action();
        $action->setSite($site);
        $action->setTask(
            $this->getDoctrine()
                 ->getRepository('MainBundle:Task')
                 ->findOneBy(array('verb' => 'review'))
        );
        $action->setPerformer($user);
        $action->setPerformed(new \DateTime("now"));
        $action->setIp($_SERVER['REMOTE_ADDR']);
        $action->setComments($site->getStatus()->getName());
        $em->persist($action);
        $site->addAction($action);
        $site->setChecked(new \Datetime());

        $em->flush();
        $redirectUrl = $this->generateUrl('view_site_compliance', array('id' => $site->getId()));
        return $this->redirect($redirectUrl);
    }

    die("failed");
}

statusChange.txt.twig(电子邮件模板)

We have updated the status of the following site to {{ site.status.name }}

Name: {{ site.title }}
URL: {{ site.url }}

For more information about your site's status, you can view the compliance report available at {{ url('view_site', {'id': site.id, 'tab': 'status'}) }}

感谢任何帮助!

(附加信息)
EmailDispatcher.php

public function sendStatusChangeEmail(MainBundle\Entity\Site $site)
{
    foreach ($site->getOwners() as $owner) {
        $message = \Swift_Message::newInstance()
            ->setSubject('[A-Z Manager] Status Change: '.$site->getTitle())
            ->setSender('redacted@google.com')
            ->setFrom('redacted@google.com')
            ->setTo($owner->getEmailAddress())
            ->setBody(
                $this->templateEngine->render(
                    'MainBundle:Email:statusChange.txt.twig',
                    array('site' => $site)
                )
            )
        ;
        $this->mailer->send($message);
    }
}

StatusManager.php(getNextStatus function)

public function getNextStatus($site)
{
    $status = $site->getStatus();
    $reviewStatus = $site->getReviewStatus();

    // Don't change the status if we still needs to review
    if (strpos($reviewStatus->getMessage(), 'Awaiting review') !== 0) {

        // Let a special status override a "natural" status
        if ($site->getSpecialStatus()) {
            $status = $site->getSpecialStatus();
        } else {
            $compliance = $site->getCompliance();
            $fail = 0;
            foreach ($compliance as $report) {
                $fail += +($report->getValue() === 0 && $report->getCriterion()->getCategory()->getName() != 'mobile');
            }
            $statusName = ($fail > 0) ? 'Failed' : 'Compliant';
            $status = $this->em->getRepository('MainBundle:Status')
                           ->findOneBy(array('name' => $statusName));
        }
    }

    return $status;
}

0 个答案:

没有答案