我在Symfony 2.8項目中使用RabbitMQBundle,我想在發佈RabbitMQ消息之前使用自定義生成器類,它在數據庫中保留實體(消息)。如何將EntityManager訪問到RabbitMQBundle自定義生產者類中?
我config.yml定義的自定義製作類:
old_sound_rabbit_mq:
...
producers:
myproducer:
class: AppBundle\Services\GenericProducer
connection: default
exchange_options: {name: 'my_exchange', type: direct}
而定製生產類:
<?php
namespace AppBundle\Services;
use AppBundle\Entity\Message;
use OldSound\RabbitMqBundle\RabbitMq\Producer;
/**
* Customised Producer, that publishes AMQP Messages
* but also:
* - writes an entry in the Message table
*/
class GenericProducer extends Producer
{
/**
* Entity Manager
*/
protected $em;
public function setEntityManager($entityManager)
{
$this->em = $entityManager;
return $this;
}
/**
* Publishes the message and merges additional properties with basic properties
* And also:
* - writes an entry in the Message table
*
* @param string $action
* @param array $parameters
* @param string $routingKey
* @param array $additionalProperties
* @param array|null $headers
*/
public function publish($action, $parameters = array() , $routingKey = '', $additionalProperties = array(), array $headers = null)
{
$message = new Message();
$message->setAction($action)
->setParameters($parameters);
$this->em->persist($message);
$this->em->flush();
$msgBody = array(
'action' => $action,
'parameters' => $parameters
);
parent::publish($msgBody, $routingKey, $additionalProperties, $headers);
}
}
我怎麼能做出GenericProducer->setEntityManager
的調用,如生產者沒有定義在services.yml,像其他服務?
是否有另一種方法來實現這一目標?
謝謝你的時間。
謝謝,我跟着裝飾方式。我在我的答案中顯示完整的代碼。 – scandel