通过FOSRestBundle RESTful API发布集合无法正常工作

时间:2016-04-10 11:19:43

标签: angularjs restangular fosrestbundle symfony-2.7

我有2个表单,用户和个人资料状态之间存在多对多关系。一切正常但我无法使用角度的json对象使用以下代码发布集合:

/*
 * Update Status
 */
$scope.updateProfilestatus = function () {
    var user = [];
    user.push($scope.user.id);
    var status =
    {
        body: $scope.status,
        enabled: 1,
        user: user
    };

    alert('Tests');
    console.log(status);
    TGWebService.profilestatuses.getAll.post(status).then(function (result) {
        console.log(result);
        alert("New Status Created");
    });
};

使用下面的类生成具有200 HTTP Status的响应,但在数据库中不会创建用户和profilestatuses之间的关联。我可以解析用户并取消序列化对象并以编程方式添加它们,但我希望能有一个魔术解决方案。

ProfileStatusesType.php

class ProfileStatusesType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('body')
            ->add('created',
                'datetime', array('widget' => 'single_text', 'date_format' => 'YYYY-MM-DD hh:mm:ss'))

            ->add('updated',
                'datetime', array('widget' => 'single_text', 'date_format' => 'YYYY-MM-DD hh:mm:ss'))
            ->add('enabled')
            ->add('user')
        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'TeamGraduate\APIBundle\Entity\ProfileStatuses'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'teamgraduate_apibundle_profilestatuses';
    }
}

users.php

class UsersType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('firstName')
            ->add('lastName')
            ->add('birthdate',
                'datetime', array('widget' => 'single_text', 'date_format' => 'YYYY-MM-DD'))
            ->add('created',
                'datetime', array('widget' => 'single_text', 'date_format' => 'YYYY-MM-DD hh:mm:ss'))
            ->add('updated',
                'datetime', array('widget' => 'single_text', 'date_format' => 'YYYY-MM-DD hh:mm:ss'))
            ->add('profileStatus')
        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'TeamGraduate\APIBundle\Entity\Users'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'teamgraduate_apibundle_users';
    }
}

修改我的ProfileStatuses.php中的代码会在下面生成以下400 HTTP状态错误:

支持馆藏的附加守则

 ->add('user',
                'collection',
                array(
                    'type'=> new UsersType(),
                    'prototype'=>true,
                    'allow_add'=>true,
                    'allow_delete'=>true,
                    'options'=>array(
                    )
                )
            )

400错误响应

{
    "code": 400,
    "message": "Validation Failed",
    "errors": {
        "errors": ["This value is not valid."],
        "children": {
            "body": {},
            "created": {},
            "updated": {},
            "enabled": {},
            "user": {
                "children": [{
                    "children": {
                        "firstName": {},
                        "middleName": {},
                        "lastName": {},
                        "birthdate": {},
                        "postalAddress": {},
                        "physicalAddress": {},
                        "idPassportNumber": {},
                        "telephone": {},
                        "mobile": {},
                        "institutionName": {},
                        "created": {},
                        "updated": {},
                        "apiKey": {},
                        "grade": {},
                        "institutionUser": {},
                        "gender": {},
                        "location": {},
                        "profileStatus": {},
                        "view": {},
                        "milestone": {},
                        "reportCard": {},
                        "device": {},
                        "badge": {},
                        "followerUser": {},
                        "inspirationalQuote": {}
                    }
                }
                ]
            },
            "cap": {}
        }
    }
}

1 个答案:

答案 0 :(得分:1)

尝试将by_reference属性用于集合类型字段

 ->add('user', 'collection', [
      'type' => new UsersType(),
      'by_reference' => false,
      'prototype' => true,
      'allow_add' => true,
      'allow_delete' => true,
  ])