0

我使用带有 FOSRestBundle 的 Symfony2 来创建 REST 服务,但是当调用 POST 方法的 createForm 函数时出现错误“ErrorHandler”。

实体

namespace Boletus\CRMBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * CommerceLog
 *
 * @ORM\Table(name="Commerce_log")
 * @ORM\Entity
 */
class CommerceLog
{
    /**
     * @var \DateTime
     *
     * @ORM\Column(name="create_at", type="datetime", nullable=false)
     */
    private $createAt;

    /**
     * @var string
     *
     * @ORM\Column(name="comments", type="text", nullable=true)
     */
    private $comments;

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var \Boletus\CRMBundle\Entity\CommerceLogType
     *
     * @ORM\ManyToOne(targetEntity="Boletus\CRMBundle\Entity\CommerceLogType")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="type_id", referencedColumnName="id")
     * })
     */
    private $type;

    /**
     * @var \Boletus\CRMBundle\Entity\Commerce
     *
     * @ORM\ManyToOne(targetEntity="Boletus\CRMBundle\Entity\Commerce")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="commerce_id", referencedColumnName="id")
     * })
     */
    private $commerce;

    /**
     * @var \Boletus\CRMBundle\Entity\Userback
     *
     * @ORM\ManyToOne(targetEntity="Boletus\CRMBundle\Entity\Userback")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="commercial_id", referencedColumnName="id")
     * })
     */
    private $commercial;

    /**
     * @var \Boletus\CRMBundle\Entity\CommerceLogChannel
     *
     * @ORM\ManyToOne(targetEntity="Boletus\CRMBundle\Entity\CommerceLogChannel")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="channel_id", referencedColumnName="id")
     * })
     */
    private $channel;



    /**
     * Set createAt
     *
     * @param \DateTime $createAt
     * @return CommerceLog
     */
    public function setCreateAt($createAt)
    {
        $this->createAt = $createAt;

        return $this;
    }

    /**
     * Get createAt
     *
     * @return \DateTime 
     */
    public function getCreateAt()
    {
        return $this->createAt;
    }

    /**
     * Set comments
     *
     * @param string $comments
     * @return CommerceLog
     */
    public function setComments($comments)
    {
        $this->comments = $comments;

        return $this;
    }

    /**
     * Get comments
     *
     * @return string 
     */
    public function getComments()
    {
        return $this->comments;
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set type
     *
     * @param \Boletus\CRMBundle\Entity\CommerceLogType $type
     * @return CommerceLog
     */
    public function setType(\Boletus\CRMBundle\Entity\CommerceLogType $type = null)
    {
        $this->type = $type;

        return $this;
    }

    /**
     * Get type
     *
     * @return \Boletus\CRMBundle\Entity\CommerceLogType 
     */
    public function getType()
    {
        return $this->type;
    }

    /**
     * Set commerce
     *
     * @param \Boletus\CRMBundle\Entity\Commerce $commerce
     * @return CommerceLog
     */
    public function setCommerce(\Boletus\CRMBundle\Entity\Commerce $commerce = null)
    {
        $this->commerce = $commerce;

        return $this;
    }

    /**
     * Get commerce
     *
     * @return \Boletus\CRMBundle\Entity\Commerce 
     */
    public function getCommerce()
    {
        return $this->commerce;
    }

    /**
     * Set commercial
     *
     * @param \Boletus\CRMBundle\Entity\Userback $commercial
     * @return CommerceLog
     */
    public function setCommercial(\Boletus\CRMBundle\Entity\Userback $commercial = null)
    {
        $this->commercial = $commercial;

        return $this;
    }

    /**
     * Get commercial
     *
     * @return \Boletus\CRMBundle\Entity\Userback 
     */
    public function getCommercial()
    {
        return $this->commercial;
    }

    /**
     * Set channel
     *
     * @param \Boletus\CRMBundle\Entity\CommerceLogChannel $channel
     * @return CommerceLog
     */
    public function setChannel(\Boletus\CRMBundle\Entity\CommerceLogChannel $channel = null)
    {
        $this->channel = $channel;

        return $this;
    }

    /**
     * Get channel
     *
     * @return \Boletus\CRMBundle\Entity\CommerceLogChannel 
     */
    public function getChannel()
    {
        return $this->channel;
    }
}

实体类型

namespace Boletus\CRMBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class CommerceLogType extends AbstractType
{
        /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('createAt')
            ->add('comments')
            ->add('type')
            ->add('commerce')
            ->add('commercial')
            ->add('channel')
        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Boletus\CRMBundle\Entity\CommerceLog',
            'csrf_protection' => false,
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'commercelog';
    }
}

我的控制器

<?php

namespace Boletus\CRMBundle\Controller;


use Boletus\CRMBundle\Entity\Commerce;
use Boletus\CRMBundle\Entity\CommerceLog;

use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\Controller\Annotations\View;
use FOS\RestBundle\Util\Codes;
use JMS\Serializer\SerializationContext;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;

class CommerceLogController extends FOSRestController {

    /**
     * @return array
     * @View()
     */
    public function getCommercelogsAction()
    {
        $commerceLogs = $this->getDoctrine()->getRepository('BoletusCRMBundle:CommerceLog')
            ->findAll();

        return array('commerce-logs' => $commerceLogs);
    }

    /**
     * @param CommerceLog $commerceLog
     * @return array
     * @View
     * @ParamConverter("commerceLog", class="BoletusCRMBundle:CommerceLog")
     *
     */
    public function getCommercelogAction(CommerceLog $commerceLog)
    {
        return array('commerce-log' => $commerceLog);
    }

    /**
     * @param Commerce $commerce
     * @return array
     * @View
     *
     */
    public function getCommercelogCommerceAction(Commerce $commerce)
    {
        $commerceLogs = $this->getDoctrine()->getRepository('BoletusCRMBundle:CommerceLog')
            ->findBy(array('commerce' => $commerce->getId()));
        return array('commerce-logs' => $commerceLogs);
    }

    public function postCommercelogAction()
    {
        return $this->processForm( new CommerceLog() );
    }

    /**
     * @param CommerceLog $commercelog
     * @return response
     * @View
     * @ParamConverter("commercelog", class="BoletusCRMBundle:CommerceLog")
     *
     */
    private function processForm ( CommerceLog $commercelog )
    {
        if( !$commercelog->getId() ) {
            $statusCode = Codes::HTTP_CREATED;
        } else {
            $statusCode = Codes::HTTP_OK;
        }

        $form = $this->createForm( new CommerceLogType(), $commercelog );
        $form->bind( $this->getRequest() );

        if ( $form->isValid() ) {
            $em = $this->getDoctrine()->getManager();
            $em->persist( $commercelog );
            $em->flush();

            $context = new SerializationContext();
            $context->setSerializeNull( true );

            $serializer = $this->get( 'jms_serializer' );

            $response = new Response( $serializer->serialize( $commercelog, 'json', $context ), $statusCode );
            $response->headers->set( 'Content-Type', 'application/json' );

            return $response;
        }

        return new Response( $form, Codes::HTTP_BAD_REQUEST );
    }
}

POST方法中的json数据

{"commercelog":{"type":4,"commerce":2,"commercial":1,"channel":3}}

数据库中的所有数据默认为空。任何人都可以帮助我吗?

4

1 回答 1

0

是的,问题是表单名称。如果您将“commercelog”作为表单名称,您有两个选项:

a) 更改 commercelog_eh {"commercelog_type":4,"commercelog_commerce":2,"commercelog_commercial":1,"commercelog_channel":3} 中的 json 字段

b) 在函数 getName() 中返回一个空字符串。

于 2014-01-15T12:16:08.253 回答