Symfony的PUT不包含映射的所有实体属性 [英] Symfony PUT does not contain all entity properties mapped

查看:132
本文介绍了Symfony的PUT不包含映射的所有实体属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用FOSRestBundle来管理我的应用程序的API。所以,我得到了几乎所有的工作,除了PUT方法,其中有些属性为NULL,但他们不应该因为实体(编号105),其中已填写。

I am using FOSRestBundle to manage the API in my app. So I got almost everything working, except PUT method, where some of the properties are NULL but they shouldn't since the entity (id 105) has them filled in.

在putAction看看:

Take a look at the putAction:

    /**
     * @ParamConverter("user", class="SoftwareBundle:User", options={"mapping": {"user": "guid"}})
     */
    public function putAction(Request $request, $user)
    {
        $form = $this->createForm(new UserType(), $user, ['method' => 'PUT']);
        $form->handleRequest($request);

        var_dump($user->getId()); // 105
        var_dump($user->getGuid()); // 12345
        var_dump($user->getEmailAddress()); // NULL (should be johndoe@example.com
        var_dump($user->getFirstName()); // NULL (should be John)
        var_dump($user->getLastName()); // NULL (should be Doe)
        var_dump($user->getSex()); // NULL (should be 0)
        var_dump($user->getPassword()); // pass123
        var_dump($user->getUserAgent()); // curl/7.30.0
        var_dump($user->getIpAddress()); // 127.0.0.1
        var_dump($user->getMember()->getPrivateNotes()); // example note

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

            return $this->view([
                'user' => $user
            ]);
        }

        return $this->view($form, Codes::HTTP_BAD_REQUEST);
    }

问题是,为什么名字,姓氏,EmailAddress的和性欲是NULL?他们真的不应该。

Question is, why FirstName, LastName, EmailAddress and Sex are NULL? They really shouldn't.

下面的形式看起来的样子:

Here is how the form looks like:

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,
                'constraints' => [
                    new HasUsername($this->getOption('uuid'))
                ]
            ])
            ->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
            ])
            ->add('member', new MemberType(), [])
        ;
    }

    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';
    }

验证是在一个单独.yml文件完成

Validation is done in a separate .yml file.

这是我送把行动JSON:

This is the JSON I am sending to put action:

{"user":{"member": {"privateNotes":"example note"}}}"

任何想法?

推荐答案

我已经解决了它过去与事件侦听器。关键是要重写默认值,如果提交的值

I've solved it in past with event listeners. The trick is to rewrite the value for defaults if submitted value is null:

namespace Acme\Bundle\UserBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;

class UserType extends AbstractType
{
    /**
     * @param FormBuilder $builder
     * @param array       $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('email')
            ->add('name');

        $callback = function(FormEvent $event) {
            if (null === $event->getData()) {
                $event->setData($event->getForm()->getData());
            }
        };

        $builder->get('email')->addEventListener(FormEvents::PRE_SUBMIT, $callback);
        $builder->get('name')->addEventListener(FormEvents::PRE_SUBMIT, $callback);
    }

   // [ ... ]
}

这篇关于Symfony的PUT不包含映射的所有实体属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆