FOSRestBundle发布了多对一的关系

时间:2015-03-17 01:35:15

标签: api rest symfony fosrestbundle

我想知道当Entity在FOSRestBundle中有另一个ManyToOne关系时如何正确发布数据。

用户实体具有语言环境(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."]}}}}

如您所见,语言环境仍然是错误的。

有谁知道我怎样才能正确发布?

修改

以下是表单的外观:

<?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 个答案:

答案 0 :(得分:2)

尝试不使用“1”且仅使用1,否则可以将其解释为字符串。

编辑:

{

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

}