如何使用Symfony将photos
集合反序列化到Photo
实体?
这是我的json:
{
"id": 3,
"status": 1,
"title": "sometitle",
"agency": {
"id": 1,
"name": "AgencyName",
...
"created_at": "-0001-11-30T00:00:00+01:00",
"updated_at": "-0001-11-30T00:00:00+01:00"
},
"price": "123123",
...
"created_at": "-0001-11-30T00:00:00+01:00",
"updated_at": "-0001-11-30T00:00:00+01:00",
"photos": [
{
"id": 1,
"path": "img1.jpg",
"created_at": "2018-04-05T15:29:47+02:00",
"updated_at": "2018-04-05T15:29:47+02:00"
},
{
"id": 2,
"path": "img2.jpg",
"created_at": "2018-04-05T15:35:28+02:00",
"updated_at": "2018-04-05T15:35:28+02:00"
}
]
}
如何在Photo
实体中指定它应该多次使用该集合?
这不起作用:
\Entity\Photo.php
/**
* @ORM\ManyToOne(targetEntity="House", inversedBy="photos")
*/
private $house;
\Entity\House.php
/**
* @ORM\OneToMany(targetEntity="Photo", mappedBy="house")
*/
private $photos;
当我在我的观点中称呼它时:
{% for photo in house.photos %}
<li>{{ photo.path }}</li>
{% endfor %}
它正在抛出异常,并且正在尝试将另一个GET请求发送到API中的不同路由,而不是仅仅使用嵌套集合
答案 0 :(得分:1)
您可以使用Symfony Serializer Component:https://symfony.com/doc/3.4/components/serializer.html
示例:
$house = $serializer->deserialize($json, House::class, 'json');