FOSMessageBundle throws an exception "Message must be a NewThreadMessage instance, "CBundle\Entity\Message\NewThreadMessage" given "

时间:2016-08-31 12:11:36

标签: php symfony

I am bit stuck on my code while using FOSMessageBundle. I have a newThreadAction like

/**
 * 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('CBundle:Message:start.html.twig', array(
        'form' => $form->createView(),
        'data' => $form->getData()
    ));

}

and my form handler is

 namespace CBundle\Form\Handler;
 use FOS\MessageBundle\FormModel\AbstractMessage;
 use CBundle\Entity\Messsage\NewThreadMessage;
 use FOS\MessageBundle\FormHandler\NewThreadMessageFormHandler as cBaseHandler;
 class NewThreadMessageFormHandler extends cBaseHandler
 {
     /**
      * 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) {
    // 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())
        ->setSubject(" ")
        ->addRecipient($message->getRecipient())
        ->setSender($this->getAuthenticatedParticipant())
        ->setBody($message->getBody())
        ->getMessage();
}

} So when I submit the form of the Message, I got an error like

    Message must be a NewThreadMessage instance, "CBundle\Entity\Message\NewThreadMessage" given

My config.yml is

fos_message:
  db_driver: orm
  thread_class: AppBundle\Entity\Message\Thread
  message_class: AppBundle\Entity\Message\Message
  new_thread_form:
        type: app_user_new_thread.new_thread_form.type
        handler: app_user_new_thread_form.handler
        # handler: fos_message.new_thread_form.handler
        # model: FOS\MessageBundle\FormModel\NewThreadMessage
        model: CBundle\Entity\Message\NewThreadMessage
        # validation_groups: [ConnThread]

Please help me and thanks in advance

1 个答案:

答案 0 :(得分:0)

我自己发现了这个问题。这是因为use语句中的错误路径 我想将语句用作use CBundle\Entity\Message\NewThreadMessage; 但我这样做use CBundle\Entity\Messsage\NewThreadMessage;请查看所包含类的路径

感谢所有观看过该问题的人