2016-04-10 9 views
0

ユーザーとプロファイルのステータスが多対多の関係にある2つのフォームがあります。すべてが正常に動作しますが、私は次のコードで角度からJSONオブジェクトを使用してコレクションを投稿することができません。FOSRestBundle経由でコレクションを投稿するRESTful APIが機能しない

/* 
* 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ステータスとの応答を生成するが、ユーザーとprofilestatusesとの間に関連があるされていないとして、データベースに作成されます。私はおそらくユーザーを解析し、オブジェクトをシリアル化してプログラムで追加することができますが、私は魔法の弾丸解決策を望んでいます。コレクションをサポートするための

追加コード:以下、次の400 HTTPステータスエラーが発生し、私のProfileStatuses.phpにコードを変更する

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

->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

コレクション型のフィールドの属性by_reference

->add('user', 'collection', [ 
     'type' => new UsersType(), 
     'by_reference' => false, 
     'prototype' => true, 
     'allow_add' => true, 
     'allow_delete' => true, 
    ]) 
を使用してみてください
関連する問題