2017-06-08 8 views
0

私は何が間違っているのか理解しようとしています。私は、角度サービスを介して投稿要求を送信しようとしている400悪い要求を取得しています。私は2つのエンティティを持っています - DocumentとDocumentCategory(多対多の関係)。私は問題なしで(カテゴリーなしで)文書そのものを投稿することができます。400悪いリクエストSymfony 3 with Angular

文書create.component.ts

createDocument(title, body, categories) { 
    let document = {title: title, body: body, categories: categories}; 
    this._crudService.createDocument(document).subscribe(
     data => { 
      return true; 
     }, 
     error => { 
      console.error("Error saving document! " + error); 
      return Observable.throw(error); 
     } 
    ); 
} 

crudService.ts

createDocument(document) { 
    let headers = new Headers({'Content-Type': 'application/json'}); 
    let options = new RequestOptions({headers: headers}); 
    //let body = JSON.stringify(document); 
    let body = document; 
    return this.http.post 
     ('http://localhost:8000/documents', body, headers); 
} 

形態

public function buildForm(FormBuilderInterface $builder, array $options) { 
    $builder 
      ->add('title', TextType::class) 
      ->add('body', TextType::class) 
      //->add('categories', TextType::class) 
      ->add('categories', EntityType::class, array(
       'class' => 'AppBundle:DocumentCategory', 
       'multiple' => true, 
       'expanded' => true, 
       'by_reference' => false, 
       'choice_label' => 'id', 
      )) 
    ; 
} 

Document.php

/** 
* @ORM\ManyToMany(targetEntity="DocumentCategory", mappedBy="documents") 
* @JMSSerializer\Expose 
*/ 
private $categories; 

DocumentCategory.php

/** 
* @ORM\ManyToMany(targetEntity="Document", inversedBy="categories") 
* @ORM\JoinTable(name="document_category_document") 
* @JMSSerializer\Expose 
*/ 
private $documents; 

要求

POST /文書HTTP/1.1 受け入れ:アプリケーション/ JSON、text/plainで、/ アクセプトエンコーディング:gzip、deflate、br のAccept-言語:EN-US、EN; Q = 0.8 接続:キープアライブ のContent-Length:213 のContent-Type:アプリケーション/ JSON ホスト:localhostを:8000 原産地:http://localhost:4200 リファラー:http://localhost:4200/admin/document/create ユーザー-Agent:Mozilla/5.0(X11; Linuxのx86_64で)のAppleWebKit/537.36(KHTML、ヤモリなど)クローム/ 55.0.2883.87サファリ/ 537.36 X-PHP-OB-レベル:1

{ 
    "title": "t", 
    "body": "<p>b</p>", 
    "categories": [ 
    { 
     "id": 1, 
     "name": "cat1", 
     "documents": [] 
    }, 
    { 
     "id": 2, 
     "name": "cat2", 
     "documents": [] 
    } 
    ] 
} 

私はカテゴリを削除すると、すべての作品言ったように。私は、アプリケーション/ JSONとしてJSON上で送信しようとすると、郵便配達応答でこれを示しています:私は

EDITは:(それを把握することはできません。私は最終的にそれを作ったlooong時間後

{ 
    "children": { 
    "title": {}, 
    "body": {}, 
    "categories": { 
     "errors": [ 
     "This value is not valid." 
     ], 
     "children": { 
     "1": {}, 
     "2": {}, 
     "3": {}, 
     "4": {} 
     } 
    } 
    } 
} 
+0

郵便配達員のようなものを使用して、バックエンドにデータを送信してください。これは、それが角度問題かバックエンドかどうかを指定します。私はそのバックエンドが関連していると仮定します。 – hogan

+0

編集を参照してください - カテゴリが有効でない理由 – Tompo

+0

バックエンドにする必要があります - jsonは有効ですが、ドキュメントとカテゴリを別々に投稿できますが、参照はできません。 – Tompo

答えて

0

ここに私の結果です場合に誰かがsimmilar問題を抱えていることが最もエレガントな解決策ではないと私はより良いを見つけるためにしようとしますが、少なくともそれが動作に問題がコントローラに今ポストメソッドは次のようになりました:。。。

public function postAction(Request $request) { 
    $form = $this->createForm(DocumentType::class, null, [ 
     'csrf_protection' => false, 
    ]); 
    $form->submit($request->request->all()); 
    if (!$form->isValid()) { 
     return $form; 
    } 

    $em = $this->getDoctrine()->getManager(); 
    $document = $form->getData(); 
    $categories = $request->request->get('categories'); 

    foreach ($categories as $categoryId) { 
     $category = $em->getRepository('AppBundle:DocumentCategory')->find((int)$categoryId['id']); 
     $category->addDocument($document); 
     $document->addCategory($category); 
     $em->persist($category);    
    } 

    $em->persist($document); 
    $em->flush(); 

    $routeOptions = [ 
     'id' => $document->getId(), 
     '_format' => $request->get('_format'), 
    ]; 

    return $this->routeRedirectView('get_document', $routeOptions, Response::HTTP_CREATED); 
} 

私の書式は簡単です:

public function buildForm(FormBuilderInterface $builder, array $options) { 
    $builder 
      ->add('title', TextType::class) 
      ->add('body', TextType::class) 
    ;   
} 
関連する問題