【发布时间】:2019-06-06 13:34:26
【问题描述】:
我有两个实体,Question 和 Alternative,其中 Question 与 Alternative 具有 OneToMany 关系,我正在尝试发送带有 嵌套文档的 JSON通过 POST 到 Question API 平台来替代。
API 平台在下面返回该错误:
Nested documents for "alternatives" attribute are not allowed. Use IRIs instead.
搜索它我发现有些人说只有使用 IRI 才有可能,而其他一些人说可以使用非规范化和规范化上下文来解决这个问题,但我找不到有关它的示例或教程。
TL;DR;
有没有办法在不使用 IRI 的情况下将嵌套关系发送到 API 平台上的实体 POST?
更新:
如题,请看下方 Question 和 Alternative 实体的两个映射。
问题
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="App\Repository\QuestionRepository")
* @ApiResource()
*/
class Question implements CreatedAtEntityInterface, UpdatedAtEntityInterface
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Token", inversedBy="questions")
* @ORM\JoinColumn(nullable=false)
* @Assert\NotBlank()
*/
private $token;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Question", inversedBy="question_versions")
*/
private $question;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Question", mappedBy="question")
*/
private $question_versions;
/**
* @ORM\Column(type="integer")
* @Assert\NotBlank()
*/
private $version;
/**
* @ORM\Column(type="string", length=100)
* @Assert\Length(max="100")
* @Assert\NotBlank()
*
*/
private $name;
/**
* @ORM\Column(type="integer")
* @Assert\NotBlank()
*/
private $type;
/**
* @ORM\Column(type="text", length=65535)
* @Assert\NotBlank()
* @Assert\Length(max="65535")
*/
private $enunciation;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Assert\Length(max="255")
*/
private $material;
/**
* @ORM\Column(type="text", length=65535, nullable=true)
* @Assert\Length(max="65535")
*/
private $tags;
/**
* @ORM\Column(type="boolean")
* @Assert\NotNull()
*/
private $public;
/**
* @ORM\Column(type="boolean")
* @Assert\NotNull()
*/
private $enabled;
/**
* @ORM\Column(type="datetime")
* @Assert\DateTime()
*/
private $createdAt;
/**
* @ORM\Column(type="datetime")
* @Assert\DateTime()
*/
private $updatedAt;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Alternative", mappedBy="question")
*/
private $alternatives;
/**
* @ORM\OneToMany(targetEntity="App\Entity\QuestionProperty", mappedBy="question")
*/
private $properties;
/**
* @ORM\OneToMany(targetEntity="App\Entity\QuestionAbility", mappedBy="question")
*/
private $abilities;
/**
* @ORM\OneToMany(targetEntity="QuestionCompetency", mappedBy="question")
*/
private $competencies;
}
替代方案
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="App\Repository\AlternativeRepository")
* @ORM\Table(name="alternatives")
* @ApiResource()
*/
class Alternative implements CreatedAtEntityInterface, UpdatedAtEntityInterface
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Question", inversedBy="alternatives")
* @ORM\JoinColumn(nullable=false)
* @Assert\NotBlank()
*/
private $question;
/**
* @ORM\Column(type="text", length=65535)
* @Assert\NotBlank()
* @Assert\Length(max="65535")
*/
private $enunciation;
/**
* @ORM\Column(type="datetime")
* @Assert\DateTime()
*/
private $createdAt;
/**
* @ORM\Column(type="datetime")
* @Assert\DateTime()
*/
private $updatedAt;
/**
* @ORM\OneToMany(targetEntity="App\Entity\AlternativeProperty", mappedBy="alternatives")
*/
private $properties;
}
【问题讨论】:
-
请更新您的问题以包含两个实体的 api-platform 映射。 序列化上下文示例见api-platform.com/docs/core/serialization/…
-
@fyrye 我已经按照你的要求更新了问题——Bruno de Souza Rocha 刚刚编辑
-
您没有使用任何序列化程序组或任何其他上下文?
-
不,我只使用api资源
-
这个 SymfonyCast 视频向我展示了如何使用
denormalizationContext注释来解决这个问题! symfonycasts.com/screencast/api-platform/collections-create
标签: php symfony api-platform.com