Symfony2 FOSRestBundle PUT Action FORM返回空结果

Dim*_*dis 8 php symfony-forms symfony fosrestbundle

我正在使用Symfony 2.2和最新版本的FOSRestBundle.所以我设法让大多数动作都有效,但我似乎遇到了FormBuilder的问题,我正在通过PUT调用请求.

我检查了请求对象,它来自我的Backbone.je模型​​,因为它应该(.save())但是在绑定到表单后,实体返回只有id导致flush()抛出错误,因为必需的字段没有填补.

控制器中的操作:

header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS ');
header('Allow GET, POST, PUT, DELETE, OPTIONS ');
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: Content-Type, *');

use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Routing\ClassResourceInterface;
use FOS\Rest\Util\Codes;
use Symfony\Component\HttpFoundation\Request;
use Greenthumbed\ApiBundle\Entity\Container;
use Greenthumbed\ApiBundle\Form\ContainerType;

class ContainerController extends FOSRestController implements ClassResourceInterface
{
/**
 * Put action
 * @var Request $request
 * @var integer $id Id of the entity
 * @return View|array
 */
public function putAction(Request $request, $id)
{
    $entity = $this->getEntity($id);
    $form = $this->createForm(new ContainerType(), $entity);
    $form->bind($request);

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

        return $this->view(null, Codes::HTTP_NO_CONTENT);
    }

    return array(
        'form' => $form,
    );
}

/**
 * Get entity instance
 * @var integer $id Id of the entity
 * @return Container
 */
protected function getEntity($id)
{
    $em = $this->getDoctrine()->getManager();

    $entity = $em->getRepository('GreenthumbedApiBundle:Container')->find($id);

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find Container entity');
    }

    return $entity;
}
Run Code Online (Sandbox Code Playgroud)

被称为的表格:

namespace Greenthumbed\ApiBundle\Form;

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

class ContainerType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name')
            ->add('description')
            ->add('isVisible')
            ->add('type')
            ->add('size')
            ->add('creationDate')
            ->add('userId')
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Greenthumbed\ApiBundle\Entity\Container',
            'csrf_protection' => false,
        ));
    }

    public function getName()
    {
        return 'greenthumbed_apibundle_containertype';
    }
}
Run Code Online (Sandbox Code Playgroud)

到目前为止我已经尝试了所有内容,但我对Symfony还是比较新的,我无法理解为什么$实体不包含请求收到的值.

仅供参考:我尝试手动操作,例如在实例化具有请求ID的Container类,并使用setter将值输入到其中并且它工作正常,我只是想以正确的方式做事情,因为Symfony建议它应该完成.

非常感谢你提前.

ebo*_*and 8

尝试在putAction中更改以下行:

$form = $this->createForm(new ContainerType(), $entity);
Run Code Online (Sandbox Code Playgroud)

至:

$form = $this->createForm(new ContainerType(), $entity, array('method' => 'PUT'));
Run Code Online (Sandbox Code Playgroud)


rol*_*ree 6

我认为你遇到了同样的问题:错误在于Form的名称.

在表单定义中,名称为"greenthumbed_apibundle_containertype".public function getName(){return'greenthumbed_apibundle_containertype'; }

因此,要将请求绑定到此表单,json应该如下所示:

{"greenthumbed_apibundle_containertype": [{"key": "value"}]}
Run Code Online (Sandbox Code Playgroud)

由于Backbone .save()方法提供了这种json

{"key":"value","key2":"value2"}
Run Code Online (Sandbox Code Playgroud)

你必须从表单中删除名称:

public function getName()
{
    return '';
}
Run Code Online (Sandbox Code Playgroud)

一般来说,如果你想发布一个像占位符一样的json

"something":{"key":"value"}
Run Code Online (Sandbox Code Playgroud)

你的表格名称必须是"某事"

从我自己的问题在这里


Nic*_*ich 0

使用ParamConverter将实体作为参数自动注入到您的方法中。

use Greenthumbed\ApiBundle\Entity\Container;

// ...

public function putAction(Request $request, Container $container)
{
    $form = $this->createForm(new ContainerType(), $container);
    $form->bind($request);

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

        // Entity already exists -> no need to persist!
        // $em->persist($entity);   

        $em->flush();

        return $this->view(null, Codes::HTTP_NO_CONTENT);
    }

    return array('form' => $form);
}
Run Code Online (Sandbox Code Playgroud)