有没有办法将对象从每个循环传递到swiftmailer消息中?

时间:2019-03-28 16:54:14

标签: php arrays loops symfony swiftmailer

我正在遍历数据库并查询对象数组,但是,当我尝试将这些对象从循环传递到swiftmail时,我只会得到最后一个。我想念什么?

我尝试做不同的操作以在循环中移动他的邮件对象,但是它发送了两封我不希望的电子邮件。我需要以某种方式将所有对象保存到数组中,然后将它们调用到swiftmailer中。

$qb = $repository->createQueryBuilder('i');
$qb->select('j.fund_code', 'j.amount', 'i.source_code', 'i.keyword', 'i.created_at', 'i.status', 'g.transaction_id')
        ->join('i.gateway_response', 'g')
        ->join('i.items', 'j')
        ->Where("i.status = :status")
        ->andWhere($qb->expr()->between('i.created_at', ':starts_at', ':ends_at'))
        ->setParameter('status', 2)
        ->setParameter('starts_at', $startsAt, \Doctrine\DBAL\Types\Type::DATETIME)
        ->setParameter('ends_at', $endsAt, \Doctrine\DBAL\Types\Type::DATETIME);

$query = $qb->getQuery();
$results = $query->getResult();
$resultCount = count($results);


if($resultCount > 0) {
    foreach($results as $holdTran) {
        $status = $holdTran['status'];
        $fundCode = $holdTran['fund_code'];
        $amount = $holdTran['amount'];
        $invoiceNumber = $holdTran['transaction_id'];
        $sourceCode = $holdTran['source_code'];
        $createdOn = $holdTran['created_at']->format('m/d/y');

    //if i vardump here, it prints all the results 

    }

    $output->writeln($resultCount . ': Transactions on Hold');

    $mailer = $this->getContainer()->get('mailer');
    $message = new \Swift_Message();
    $message->setSubject('Daily Giving Report')
            ->setFrom('chris2kus31@gmail.com')
            ->setTo('cmoreno@kcm.org')
            ->setContentType("text/html")
            ->setBody($this->getContainer()
            ->get('templating')
            ->render('@Giving/Default/givingReport.html.twig',
                [
                    'resultCount' => $resultCount, //only prints the last result
                    'status'=> $status,  //only prints the last result
                    'fundCode'=>$fundCode,  //only prints the last result
                    'amount'=>$amount,  //only prints the last result
                    'invoiceNumber'=>$invoiceNumber,//only prints the last result
                    'createdAt'=>$createdOn,  //only prints the last result
                    'sourceCode'=>$sourceCode  //only prints the last result
                ])

            );
    $mailer->send($message);

我想做的是能够循环,获取所有对象并将它们传递给Swiftmailer。然后,我想使用树枝循环并打印所有结果。如下面但带有所有结果。

<p>Transaction {{invoice}}</p>

0 个答案:

没有答案