我使用Symfony2和FOSRestBundle来创建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}}
db中的所有数据都是默认的null。 有人可以帮帮我吗?
答案 0 :(得分:0)
是的,问题是表单名称。 如果您将“commercelog”作为表单名称,则有两个选项:
a)更改commercelog_ e.h中的json字段{“commercelog_type”:4,“commercelog_commerce”:2,“commercelog_commercial”:1,“commercelog_channel”:3}
b)在函数getName()中返回一个空字符串。