gpt4 book ai didi

symfony - 与api平台的嵌入关系

转载 作者:行者123 更新时间:2023-12-02 21:24:41 24 4
gpt4 key购买 nike

我对 Api 平台有疑问。 (https://api-platform.com)我有两个实体。问题和答案。我想要进行一次 POST 调用来创建一个只有一个答案的问题。我显示我的实体。

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Serializer\Annotation\Groups;

/**
* @ApiResource(
* normalizationContext={"groups"={"question"}},
* denormalizationContext={"groups"={"question"}})
* @ORM\Entity
*/
class Question
{
/**
* @Groups({"question"})
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;

/**
* @Groups({"question"})
* @ORM\Column
* @Assert\NotBlank
*/
public $name = '';

/**
* @Groups({"question"})
* @ORM\OneToMany(targetEntity="Answer", mappedBy="question", cascade={"persist"})
*/
private $answers;

public function getAnswers()
{
return $this->answers;
}

public function setAnswers($answers): void
{
$this->answers = $answers;
}


public function __construct() {
$this->answers = new ArrayCollection();
}

public function getName(): string
{
return $this->name;
}

public function setName(string $name): void
{
$this->name = $name;
}

public function getId(): int
{
return $this->id;
}
}

和一个应答实体

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Serializer\Annotation\Groups;

/**
*
* @ApiResource
* @ORM\Entity
*/
class Answer
{
/**
* @Groups({"question"})
* @ORM\Id
* @ORM\Column(type="guid")
*/
public $id;

/**
* @Groups({"question"})
* @ORM\Column
* @Assert\NotBlank
*/
public $name = '';

/**
* @ORM\ManyToOne(targetEntity="Question", inversedBy="answers")
* @ORM\JoinColumn(name="question_id", referencedColumnName="id")
*/
public $question;

public function getQuestion()
{
return $this->question;
}

public function setQuestion($question): void
{
$this->question = $question;
}

public function getName(): string
{
return $this->name;
}

public function setName(string $name): void
{
$this->name = $name;
}

public function getId(): string
{
return $this->id;
}

public function __toString()
{
return $this->getName();
}
}

现在我可以从 nelmio 的仪表板创建一个问题并生成答案。但在数据库中,我的答案并没有保存与问题的关系。

{
"name": "my new question number 1",
"answers": [
{
"id": "ddb66b71-5523-4158-9aa3-2691cae9d473",
"name": "my answer 1 to question number 1"
}
]
}

另一个问题是...我已经通过 guid 更改了我的答案 id,因为当我创建并回答没有 id 的问题时,我收到错误。我可以创建问题和答案而不指定 ID 吗?

提前致谢

最佳答案

嗯,我在您的实体中看到一些错误...首先,由于问题和答案实体之间的关系是 OneToMany,您的 Qestion 实体应该实现此功能,即:

use ApiPlatform\Core\Annotation\ApiProperty;

//..... the rest of your code

/**
* @ApiProperty(
* readableLink=true
* writableLink=true
* )
* @Groups({"question"})
* @ORM\OneToMany(targetEntity="Answer", mappedBy="question", cascade={"persist"})
*/
private $answers;


public function __construct()
{
//....

$this->answers = new ArrayCollection();

//...
}

public function addAnswer(Answer $answer): self
{
if (!$this->answers->contains($answer)) {
$this->answers[] = $answer;
$answer->setQuestion($this)
}

return $this;
}

public function removeAnswer(Answer $answer): self
{
if ($this->answers->contains($answer)) {
$this->answers->removeElement($answer);
}
return $this;
}

命令

PHP bin/console make:entity

允许您在关系类型的实体中创建一个字段,并且只需按照说明操作即可为您创建这些方法(创建两个实体后,使用更新问题实体的命令...)

ReadLink ApiProperty 注释用于查看 GET 请求上的嵌入对象,如果您使用序列化组,则相同,如果将其设置为 false,则响应将如下所示:

{
"name": "my new question number 1",
"answers": [
"/api/answers/1",
"/api/answers/2",
....
]
}

它用于使响应更小(除其他外)...而 writableLink 用于允许像这样的 POST 请求(有关更多信息,请参阅此示例 here ):

{
"name": "my new question number 1",
"answers": [
{
"id": "ddb66b71-5523-4158-9aa3-2691cae9d473",
"name": "my answer 1 to question number 1"
}
]
}

当然,在每个实体中使用相应的序列化组......在 ApiPlatform 中,嵌入对象通过 setter 和 getters 方法进行持久化,还为 OneToMany 关系添加和删除方法,ORM 完成其余工作。让我知道这是否有帮助。干杯!

关于symfony - 与api平台的嵌入关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52806092/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com