2017-06-08 148 views
0

我想弄清楚我做錯了什麼。我收到400個錯誤的請求,試圖通過角度服務發送發佈請求。我有2個實體 - Document和DocumentCategory(多對多關係)。我可以發佈文檔本身(沒有類別),沒有問題。400錯誤請求Symfony 3與角度

文檔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,文本/純,/ Accept-Encoding:gzip,deflate,br 接受語言:EN-US,EN; Q = 0.8 連接:保持活躍 的Content-Length:213 內容類型:應用程序/ JSON 主機:本地主機:8000 產地:http://localhost:4200 的Referer:http://localhost:4200/admin/document/create 用戶-Agent:Mozilla/5.0(X11; Linux的x86_64的)爲AppleWebKit/537.36(KHTML,例如Gecko)Chrome瀏覽器/ Safari瀏覽器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:

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

請使用類似郵遞員的方式將數據發送到後端。這將指定它是否和角度問題或後端。我假設它的後端相關。 – hogan

+0

請參閱編輯 - 爲什麼類別無效? – Tompo

+0

它必須是後端--JSON是有效的,我可以發佈文檔和類別,但不參考。 – Tompo

回答

0

後looong時候,我終於做到了這是我的結果萬一有人有simmilar問題這不是最優雅的解決方案,我將嘗試找到一個更好的,但至少它的作品的問題是在控制器現在POST方法看起來是這樣的:。

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