Mysql 使用嵌套类别反序列化JSON
我的控制器类:Mysql 使用嵌套类别反序列化JSON,mysql,doctrine-orm,relationship,jmsserializerbundle,symfony-3.4,Mysql,Doctrine Orm,Relationship,Jmsserializerbundle,Symfony 3.4,我的控制器类: public function postAction(Request $request) { $content = $request->getContent(); $category = $this->get('jms_serializer')->deserialize($content,'AppBundle\Entity\Category','json'); $errors = $this->get('validator')-
public function postAction(Request $request)
{
$content = $request->getContent();
$category = $this->get('jms_serializer')->deserialize($content,'AppBundle\Entity\Category','json');
$errors = $this->get('validator')->validate($category);
if (count($errors) > 0) {
return new View("NAME LENGTH MUST BE >4",Response::HTTP_BAD_REQUEST);
} else {
$em = $this->getDoctrine()->getManager();
$em->persist($category);
$em->flush();
return new View($category, Response::HTTP_OK);
}
}
实体:
class Category
{
private $id;
private $parent;
public function getChildren()
{
return $this->children;
}
private $children;
public function __construct()
{
$this->children = new ArrayCollection();
}
//setters and getters
Doctrine.yml:
AppBundle\Entity\Category:
type: entity
oneToMany:
children:
targetEntity: AppBundle\Entity\Category
mappedBy: parent
orderBy:
name: ASC
manyToOne:
parent:
targetEntity: AppBundle\Entity\Category
inversedBy: children
joinColumn:
name: parentId
referencedColumn: id
table: category
repositoryClass: AppBundle\Repository\CategoryRepository
id:
id:
column: id
type: integer
id: true
generator:
strategy: AUTO
fields:
name:
type: string
lenght: 255
当我发送POST json请求时,如下所示:
{
"name": "Child to 8",
"parentId": "8"
}
在MySQL表中,我没有收到parentId:
mysql> select * from category;
+----+--------------------+----------+
| id | name | parentId |
+----+--------------------+----------+
| 1 | Primary Category | NULL |
| 2 | Secondary Category | 1 |
| 3 | D_child | 1 |
| 4 | F_child | 1 |
| 5 | Z_child | 1 |
| 6 | Y_child | 1 |
| 7 | H_child | 1 |
| 8 | A_child | 1 |
| 9 | Child to 8 | NULL |<----- must be 8
+----+--------------------+----------+
我知道id是一个整数,但parentId已经是类Category的对象了。但是怎样才能让他也签约呢?
我该怎么做?也许我不明白什么…您需要一个用于序列化程序的
.yml
配置文件。在您的例子中-Entity.Category.yml
。
在这个文件中,添加嵌套实体的属性,为他设置一种类型的you实体,并确保访问器(setter、getter) 我怎么能做到<代码>添加嵌套实体的属性?输出属性parentId中是否有?即使只有标量值。
{
"id": 9,
"name": "Child to 8"
}