0

FOSRestBundle で Symfony2 を使用して REST サービスを作成していますが、createForm 関数を POST メソッドに呼び出すと、「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}}

データベース内のすべてのデータはデフォルトで null です。誰でも私を助けることができますか?

4

1 に答える 1

0

はい、問題はフォーム名です。フォーム名が「commercelog」の場合、次の 2 つのオプションがあります。

a) commercelog_ eh {"commercelog_type":4,"commercelog_commerce":2,"commercelog_commercial":1,"commercelog_channel":3} の json フィールドを変更します。

b) 関数 getName() で空の文字列を返します。

于 2014-01-15T12:16:08.253 に答える