我尝试在我的Symfony Sonata软件包中提供服务,以便在创建订单后立即向特定人员发送电子邮件.发送电子邮件的人是用户选择批准订单的人.
我尝试按照Symfony网站上的服务容器文档,但对我来说感觉太不完整了.我想看一个完整的例子而不仅仅是几个片段.
到目前为止,这是我的电子邮件服务类;
<?php
namespace Qi\Bss\BaseBundle\Lib\PurchaseModule;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
use Doctrine\ORM\EntityManager;
/**
*
*/
class Notifier
{
/**
* Service container
* @var type
*/
private $serviceContainer;
public function notifier($subject, $from, $to, $body) {
$message = \Swift_Message::newInstance()
->setSubject($subject)
->setFrom($from)
->setTo($to)
->setBody($body)
;
$this->serviceContainer->get('mailer')->send($message);
}
/**
* Sets the sales order exporter object
* @param type $serviceContainer
*/
public function setServiceContainer($serviceContainer)
{
$this->serviceContainer = $serviceContainer;
}
}
Run Code Online (Sandbox Code Playgroud)
我的services.yml文件中的服务看起来像这样;
bss.pmod.order_notifier:
class: Qi\Bss\BaseBundle\Lib\PurchaseModule\Notifier
arguments: ["@mailer"] …Run Code Online (Sandbox Code Playgroud) 我有一个数组,像这样返回对象:
array (size=61)
0 =>
object(Xxx\Car)[602]
private 'id' => int 53
private 'name' => string 'Volkswagen' (length=10)
1 =>
object(Xxx\Car)[594]
private 'id' => int 43
private 'name' => string 'Toyota' (length=6)
2 =>
object(Xxx\Car)[595]
private 'id' => int 32
private 'name' => string 'BMW' (length=3)
Run Code Online (Sandbox Code Playgroud)
如何将数组转换为id成为键和描述值,如下所示:
array (size=61)
53 => 'Volkswagen'
43 => 'Toyota'
32 => 'BMW'
Run Code Online (Sandbox Code Playgroud)
我试过了
$cars = array();
foreach ($result as $key => $value) {
$cars[$value['id']] = $value['name'];
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用.