我想知道我做错了什么。我收到了400个错误的请求,尝试通过角度服务发送帖子请求。我有2个实体 - Document和DocumentCategory(多对多关系)。我可以毫无问题地发布文档本身(没有类别)。
文档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 接受:application / json,text / plain, / Accept-Encoding:gzip,deflate,br Accept-Language:en-US,en; q = 0.8 连接:保持活力 内容长度:213 Content-Type:application / json 主持人:localhost:8000 来源:http://localhost:4200 推荐人:http://localhost:4200/admin/document/create 用户代理:Mozilla / 5.0(X11; Linux x86_64)AppleWebKit / 537.36(KHTML,类似Gecko)Chrome / 55.0.2883.87 Safari / 537.36 X-Php-Ob-Level:1
{
"title": "t",
"body": "<p>b</p>",
"categories": [
{
"id": 1,
"name": "cat1",
"documents": []
},
{
"id": 2,
"name": "cat2",
"documents": []
}
]
}
正如我所说,如果删除类别,一切正常。我无法弄明白:(
编辑:当我尝试将json作为application / json发送时,Postman会在响应中显示此信息:
{
"children": {
"title": {},
"body": {},
"categories": {
"errors": [
"This value is not valid."
],
"children": {
"1": {},
"2": {},
"3": {},
"4": {}
}
}
}
}
答案 0 :(得分:0)
经过一段时间后我终于成功了。这是我的结果,以防有人出现类似问题。它不是最优雅的解决方案,我会尝试找到一个更好的解决方案,但至少它是有效的。问题出在控制器上。现在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)
;
}