Php api平台odm iri参考赢得';不能在json中转换为对象
我想创建一个这样的模型Php api平台odm iri参考赢得';不能在json中转换为对象,php,mongodb,document,api-platform.com,doctrine-odm,Php,Mongodb,Document,Api Platform.com,Doctrine Odm,我想创建一个这样的模型 { "id": 7653, "name": "Vortl 123", "category": [ { "name": "Electronic", "url": "electronic", "id": 1 }, { "name": "Phone", "url": "phone", "id": 2 }, { "name": "Mobile Phon
{
"id": 7653,
"name": "Vortl 123",
"category": [
{
"name": "Electronic",
"url": "electronic",
"id": 1
}, {
"name": "Phone",
"url": "phone",
"id": 2
},
{
"name": "Mobile Phone",
"url": "mobile-phone",
"id": 3
}
}
我使用doctrine odm引用创建了文档。代码就是这些
这是产品类别
/**
* @ApiResource
*
* @Document
*/
class Product
{
/**
* @ODM\Id(strategy="INCREMENT", type="integer")
*/
private $id;
/**
* @ODM\Field(type="string")
* @Assert\NotBlank
*/
public $name;
public function __construct()
{
$this->categories = new ArrayCollection();
}
/**
* @ODM\ReferenceMany(targetDocument=Category::class, inversedBy="product", cascade={"persist"}, storeAs="id")
*/
public $categories;
/**
* @return mixed
*/
public function getId(): ?int
{
return $this->id;
}
/**
* @param Category $category
*/
public function addCategory(Category $category): void
{
$this->categories->add($category);
}
public function removeCategory(Category $category): void
{
$category->product = null;
$this->categories->removeElement($category);
}
我的分类课是
/**
* @ApiResource
*
* @ODM\Document
*/
class Category
{
/**
* @ODM\Id(strategy="INCREMENT", type="integer")
*/
private $id;
/**
* @ODM\ReferenceOne(targetDocument=Product::class, mappedBy="categories", storeAs="id")
*/
public $product;
/**
* @ODM\Field(type="string")
* @Assert\NotBlank
*/
private $name;
/**
* @ODM\Field(type="string")
*/
private $url;
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @param mixed $id
*/
public function setId($id): void
{
$this->id = $id;
}
/**
* @return mixed
*/
public function getName()
{
return $this->name;
}
/**
* @param mixed $name
*/
public function setName($name): void
{
$this->name = $name;
}
/**
* @return mixed
*/
public function getUrl()
{
return $this->url;
}
/**
* @param mixed $url
*/
public function setUrl($url): void
{
$this->url = $url;
}
}
它工作正常。当我可以发布分类并获取它们时。然后我发布产品,并通过iri引用向产品添加类别。问题是我看到的是categories id,而不是像这样的categories对象
{
"@context": "/api/contexts/Product",
"@id": "/api/products/17",
"@type": "Product",
"name": "Product17",
"categories": [
"/api/categories/1",
"/api/categories/2",
"/api/categories/3"
]
}
我用post方法创建类别,然后用post方法创建产品。为了向产品添加类别,我在产品的post方法中使用了iri引用,如/api/categories/1
我不明白为什么它显示为iri参考?在我想要的示例中,我想要这些iri引用的对象。有人能帮我吗 您必须至少添加一个组
normalizationContext
:
<?php
/**
* @ApiResource(
* normalizationContext={"groups" = {"product:read"}}
* )
*
* @Document
*/
class Product
{
/**
* @ODM\Id(strategy="INCREMENT", type="integer")
*/
private $id;
/**
* @ODM\Field(type="string")
* @Assert\NotBlank
* @Groups({"product:read"})
*/
public $name;
// ...
}
之后,您应该看到
类别
的对象具有属性$name
,您必须至少添加一个组normalizationContext
:
<?php
/**
* @ApiResource(
* normalizationContext={"groups" = {"product:read"}}
* )
*
* @Document
*/
class Product
{
/**
* @ODM\Id(strategy="INCREMENT", type="integer")
*/
private $id;
/**
* @ODM\Field(type="string")
* @Assert\NotBlank
* @Groups({"product:read"})
*/
public $name;
// ...
}
在这之后,你应该看到
类别
的属性为$name
的对象。要做到这一点,只需向每个实体添加序列化和反序列化组。要做到这一点,只需向每个实体添加序列化和反序列化组。是的,这是doSorry的最佳方法,我是新手。我不明白逻辑。为什么在产品名称中使用组注释?我唯一的问题是分类收集。类别集合仍然带有iri引用,而不是对象,而我正在做的和你一样。我想你需要阅读文档。简言之,您需要将同一组添加到“产品”(用于所有操作或一个操作)和“类别”中您希望在答案中看到的所有属性。哦,文档和您的答案帮助我理解和解决了这些问题。谢谢。是的,这是去多索里最好的方式,我是新来的。我不明白逻辑。为什么在产品名称中使用组注释?我唯一的问题是分类收集。类别集合仍然带有iri引用,而不是对象,而我正在做的和你一样。我想你需要阅读文档。简言之,您需要将同一组添加到“产品”(用于所有操作或一个操作)和“类别”中您希望在答案中看到的所有属性。哦,文档和您的答案帮助我理解和解决了这些问题。非常感谢。