嵌入与api平台的关系
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了嵌入与api平台的关系相关的知识,希望对你有一定的参考价值。
我怀疑Api平台。 (https://api-platform.com)我有两个实体。问题和答案。我想要一个POST调用来创建一个答案的问题。我展示了我的实体。
namespace AppEntity;
use ApiPlatformCoreAnnotationApiResource;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineORMMapping as ORM;
use SymfonyComponentValidatorConstraints as Assert;
use SymfonyComponentSerializerAnnotationGroups;
/**
* @ApiResource(
* normalizationContext={"groups"={"question"}},
* denormalizationContext={"groups"={"question"}})
* @ORMEntity
*/
class Question
{
/**
* @Groups({"question"})
* @ORMId
* @ORMGeneratedValue
* @ORMColumn(type="integer")
*/
private $id;
/**
* @Groups({"question"})
* @ORMColumn
* @AssertNotBlank
*/
public $name = '';
/**
* @Groups({"question"})
* @ORMOneToMany(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 AppEntity;
use ApiPlatformCoreAnnotationApiResource;
use DoctrineORMMapping as ORM;
use SymfonyComponentValidatorConstraints as Assert;
use SymfonyComponentSerializerAnnotationGroups;
/**
*
* @ApiResource
* @ORMEntity
*/
class Answer
{
/**
* @Groups({"question"})
* @ORMId
* @ORMColumn(type="guid")
*/
public $id;
/**
* @Groups({"question"})
* @ORMColumn
* @AssertNotBlank
*/
public $name = '';
/**
* @ORMManyToOne(targetEntity="Question", inversedBy="answers")
* @ORMJoinColumn(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即可回答问题吗?
提前致谢
答案
对于第一点,它应该在数据库中持久存在而没有问题。无论如何,您可以为Question实体创建一个PostValidateSubscriber并检查是否存在关系。
<?php /** @noinspection PhpUnhandledExceptionInspection */
namespace AppEventSubscriber;
use ApiPlatformCoreEventListenerEventPriorities;
use SymfonyComponentEventDispatcherEventSubscriberInterface;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpKernelEventGetResponseForControllerResultEvent;
use SymfonyComponentHttpKernelKernelEvents;
use SymfonyComponentSecurityCoreAuthenticationTokenStorageTokenStorageInterface;
final class QuestionPostValidateSubscriber implements EventSubscriberInterface
{
private $tokenStorage;
public function __construct(
TokenStorageInterface $tokenStorage
) {
$this->tokenStorage = $tokenStorage;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => ['checkQuestionData', EventPriorities::POST_VALIDATE]
];
}
/**
* @param GetResponseForControllerResultEvent $event
*/
public function checkQuestionData(GetResponseForControllerResultEvent $event)
{
$bid = $event->getControllerResult();
$method = $event->getRequest()->getMethod();
if (!$question instanceof Question || (Request::METHOD_POST !== $method && Request::METHOD_PUT !== $method))
return;
$currentUser = $this->tokenStorage->getToken()->getUser();
if (!$currentUser instanceof User)
return;
}
}
并做一个回声或使用xdebug检查问题。
对于第二点,您可以为实体的id添加这些注释,因此id将生成自己的ID。
- @ORM GeneratedValue()
- @ORM 柱(类型= “整数”)
以上是关于嵌入与api平台的关系的主要内容,如果未能解决你的问题,请参考以下文章