FOSMessageBundle in Symfony2



我如何设置,当我创建一个线程,消息是由线程的创建者读取?

我有这部分代码

$composer = $this->get('fos_message.composer');
    $message  = $composer->newThread()
        ->setSender($this->getUser())
        ->setSubject('myThread')
        ->setBody($request->get('createThread')['my_thread']);
    $sender = $this->get('fos_message.sender');
    $sender->send($message);

但是当我在最后一行发送消息时,在数据库中is_read的值被设置为0,而发送方应该设置为1。因此,我需要将作者设置为在他发送消息时读取。

有人知道吗?:)

消息元数据在消息持久化之前不存在。这就是为什么你必须在保存消息到数据库后设置读取状态

最简单的方法是注册EventSubscriber。工作示例代码:
<?php
namespace AcmeDemoBundleEventListener;
use SymfonyComponentEventDispatcherEventSubscriberInterface;
use FOSMessageBundleEventMessageEvent;
use FOSMessageBundleEventFOSMessageEvents as Event;
use FOSMessageBundleModelManagerMessageManagerInterface;
class MessageSendSubscriber implements EventSubscriberInterface
{
    private $messageManager;
    public function __construct(MessageManagerInterface $messageManager)
    {
        $this->messageManager = $messageManager;
    }
    public static function getSubscribedEvents()
    {
        return array(
            Event::POST_SEND => 'markAsReadBySender'
        );
    }
    public function markAsReadBySender(MessageEvent $event)
    {
        $message = $event->getMessage();
        $sender = $message->getSender();
        $this->messageManager->markAsReadByParticipant($message, $sender);
        $this->messageManager->saveMessage($message);
    }
}
在services.yml:

message_send_listener:
    class: AcmeDemoBundleEventListenerMessageSendSubscriber
    arguments: [@fos_message.message_manager]
    tags:
        - { name: kernel.event_subscriber }

在这里你可以查看你可以订阅的事件:https://github.com/FriendsOfSymfony/FOSMessageBundle/blob/master/Event/FOSMessageEvents.php

相关内容

  • 没有找到相关文章

最新更新