1 回答

TA貢獻1895條經驗 獲得超3個贊
您正在搜索序列化您的商品。所以我想這更多的是序列化問題而不是子資源問題。我認為你應該使用序列化組而不是子資源。
SOLUTION1序列化嵌入關系
“嵌入關系”提供了書籍和作者的示例。
<?php
// api/src/Entity/Item.php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Serializer\Annotation\Groups;
/**
?* @ApiResource(normalizationContext={"groups"={"item"}})
?*/
class Item
{
? ? /**
? ? ?* @Groups({"item"})
? ? ?*/
? ? public $description;
? ? /**
? ? ?* @Groups({"item"})
? ? ?*/
? ? public $comment;
? ? /**
? ? ?* @Groups({"item"})
? ? ?*/
? ? public $program;
? ? // ...
}
// api/src/Entity/Person.php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Serializer\Annotation\Groups;
/**
?* @ApiResource
?*/
class Person
{
? ? /**
? ? ?* ...
? ? ?* @Groups("item") <=== This is item, not program but you can have two groups
? ? ?*/
? ? public $name;
? ? // ...
}
SOLUTION2混合子資源和序列化組
這個示例應該可以工作,但是(如果需要子資源)您也可以混合子資源和序列化,但在您的情況下,我認為第一個解決方案是最好的。
- 1 回答
- 0 關注
- 129 瀏覽
添加回答
舉報