php - FOSMessageBundle - 如何在没有收件人字段的情况下发送消息

标签 php symfony

我隐藏了我的收件人字段表单,现在我想了解在 Controller 中我可以在哪里告诉收件人的值

消息 Controller :

/**
 * Create a new message thread
 *
 * @return Response
 */
public function newThreadAction()
{
    $form = $this->container->get('fos_message.new_thread_form.factory')->create();
    $formHandler = $this->container->get('fos_message.new_thread_form.handler');

    if ($message = $formHandler->process($form)) {
        return new RedirectResponse($this->container->get('router')->generate('fos_message_thread_view', array(
            'threadId' => $message->getThread()->getId()
        )));
    }

    return $this->container->get('templating')->renderResponse('FOSMessageBundle:Message:newThread.html.twig', array(
        'form' => $form->createView(),
        'data' => $form->getData()
    ));
}

$形成数学:

class NewThreadMessageFormFactory extends AbstractMessageFormFactory
{
    /**
     * Creates a new thread message
     *
     * @return Form
     */
    public function create()
    {
        $message = $this->createModelInstance();

        return $this->formFactory->createNamed($this->formName, $this->formType, $message);
    }
}

$formHandler 匹配到:

class NewThreadMessageFormHandler extends AbstractMessageFormHandler
{
    /**
     * Composes a message from the form data
     *
     * @param AbstractMessage $message
     * @return MessageInterface the composed message ready to be sent
     * @throws InvalidArgumentException if the message is not a NewThreadMessage
     */
    public function composeMessage(AbstractMessage $message)
    {
        if (!$message instanceof NewThreadMessage) {
            throw new \InvalidArgumentException(sprintf('Message must be a NewThreadMessage instance, "%s" given', get_class($message)));
        }

        return $this->composer->newThread()
                    ->setSubject($message->getSubject())
                    ->addRecipient($message->getRecipient())
                    ->setSender($this->getAuthenticatedParticipant())
                    ->setBody($message->getBody())
                    ->getMessage();
    }
}

我希望有一些解决方案!

非常感谢!

最佳答案

您可以在创建表单后设置表单的表单数据。 我的表单通过以下方式解决:

在你的 newThreadAction 中:

$form = $this->getFormForNewThreadAction($account);

请记住,在我的示例中 $account 具有 ParticipantInterface。

在您的 MessageController 中:

/**
 * @param ParticipantInterface $account
 * @return \FOS\MessageBundle\FormFactory\Form|FormInterface
 */
private function getFormForNewThreadAction(ParticipantInterface $account)
{
    $form = $this->getNewThreadFormFactory()->create();
    $preSetFormData = $this->getPreSetFormData($account, $form);
    $form->setData($preSetFormData);

    return $form;
}

/**
 * @param ParticipantInterface $account
 * @param Form $form
 * @return NewThreadMessage
 */
private function getPreSetFormData(ParticipantInterface $account, Form $form)
{
    $recipients = new ArrayCollection();
    $recipients->add($account);

    /** @var NewThreadMessage $newThreadMessage */
    $newThreadMessage = $form->getData();
    $newThreadMessage->setRecipients($recipients);

    return $newThreadMessage;
}

表单需要他的 NewThreadMessage 作为数据,您可以在那里设置收件人。这就是所有人。

关于php - FOSMessageBundle - 如何在没有收件人字段的情况下发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17762757/

相关文章:

php - 当用户重命名另一个表中的数据时更新列

php - PDO 在查询中引用整数。我现在应该做什么?

Symfony 2 FOSElasticaBundle : elastic search documents are not automatically synchronized with entities : need to run ' fos:elastica:populate'

symfony - 使用 Symfony2 安装 DoctrineFixturesBundle

symfony - 自定义类中没有实体管理器和 ContextErrorException

symfony - 为 FOSUserBundle 中的用户手动发送密码重置电子邮件

php - 将 A 表中的值插入到 A 表中

php - 搜索多个表

php - 比较两个用户定义对象数组

php - 访问 Twig 中字符串变量错误的属性