FOSRestBundle post 多对一关系

FOSRestBundle post many to one relation

我想知道当实体在 FOSRestBundle 中有另一个 ManyToOne 关系时如何正确 post 数据。

用户实体具有语言环境(locale_id):

/**
 * @ORM\ManyToOne(targetEntity="Locale")
 * @ORM\JoinColumn(name="locale_id", referencedColumnName="id")
 */
private $locale;

我希望通过类似的东西:

{

    "user":{
        "firstName":"John",
        "emailAddress":"somewhere@somehow.com",
        "lastName":"Doe",
        "sex":"1",
        "locale":{
            "id":"1"
        }
    }

}

会工作,但它没有通过验证并且 Symfony 抛出:

{"code":400,"message":"Validation Failed","errors":{"children":{"firstName":[],"lastName":[],"emailAddress":[],"sex":[],"locale":{"errors":["This value is not valid."]}}}}

如您所见,区域设置仍然错误。

有谁知道我怎样才能post它正确?

编辑

表单如下所示:

<?php

namespace Software\Bundle\Form\Type;

use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;

/**
 * Class UserType
 * @package Software\Bundle\Form\Type
 */
class UserType extends AbstractFormType
{
    public function buildForm(FormBuilderInterface $builder, array $option)
    {
        $builder
            ->add('firstName', 'text', [
                'label' => 'word.first_name',
                'required' => true
            ])
            ->add('lastName', 'text', [
                'label' => 'word.last_name',
                'required' => true
            ])
            ->add('emailAddress', 'email', [
                'label' => 'word.email_address',
                'required' => true
            ])
            ->add('sex', 'choice', [
                'label' => 'word.sex',
                'choices' => [
                    '0' => 'word.male',
                    '1' => 'word.female'
                ],
                'required'    => true,
                'empty_value' => 'word.select',
                'empty_data' => null
            ])
            ->add('locale', 'entity', [
                'label' => 'word.locale',
                'required'    => false,
                'property' => 'code',
                'class' => 'SoftwareBundle:Locale',
                'query_builder' => function(EntityRepository $er) {
                        return $er->createQueryBuilder('l')
                            ->orderBy('l.code', 'ASC');
                    },
                'placeholder' => 'word.select',
                'empty_data'  => null
            ])
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults([
            'translation_domain'    => 'general',
            'data_class'            => 'Software\Bundle\Entity\User',
            'attr'                  => ['novalidate' => 'novalidate'],
            'csrf_protection'       => false
        ]);
    }

    public function getName()
    {
        return 'user';
    }
}

编辑 2

控制器:

public function postAction(Request $request)
{
    $form = $this->createForm(new UserType(), new User());
    $form->handleRequest($request);

    if($form->isValid())
    {
        die('are you valid or not??');
    }

    return $this->view($form, 400);
}

尝试不带“1”而只带 1 ,否则它会被解释为字符串。

编辑:

{

    "user":{
        "firstName":"John",
        "emailAddress":"somewhere@somehow.com",
        "lastName":"Doe",
        "sex":"1",
        "locale": 1
        }
    }

}