Symfony4 setter where getter match id route?
Symfony4 setter where getter match id route?
我是 Symfony 的新手
我正在做一个投票系统,但我想这应该适用于,
目前我的控制器功能是这样的,这只会创建一个带有 1vote 的新行,但不会更新之前创建的任何 $id。
/**
* @Route("/public/{id}/vote", name="poll_vote", methods="GET|POST")
*/
public function vote(Request $request, Poll $poll): Response
{
$inc = 1;
$em = $this->getDoctrine()->getManager();
$entity = new Poll();
$entity->setVotes($inc++);
$em->persist($entity);
$em->flush();
}
return $this->redirectToRoute('poll_public');
}
这是我的 twig 模板按钮
<a href="{{ path('poll_vote', {'id': poll.id}) }}">
这是我的实体
class Poll
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $votes;
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getVotes(): ?int
{
return $this->votes;
}
public function setVotes(?int $votes): self
{
$this->votes = $votes;
return $this;
}
}
我不知道如何从我的实体匹配我的 getID 和从@Route 匹配 $id。
如有任何指导或建议,我们将不胜感激。
谢谢
编辑:
在 Arne 回答后更新了正确的函数:
/**
* @Route("/public/{id}", name="poll_vote", methods="GET|POST")
*/
public function vote($id)
{
$entityManager = $this->getDoctrine()->getManager();
$poll = $entityManager->getRepository(Poll::class)->find($id);
if (!$poll) {
throw $this->createNotFoundException(
'No polls found for id '.$id
);
}
$poll->setVotes($poll->getVotes()+1);
$entityManager->flush();
return $this->redirectToRoute('poll_public', [
'id' => $poll->getId()
]);
}
基本上您必须从您的请求中获取 ID,查询您的投票实体的实体存储库,更新投票并将其保存回您的数据库。
从您的请求中获取 ID
$id = $request->query->get('id');
查询存储库:
$entityManager = $this->getDoctrine()->getManager();
$poll= $entityManager->getRepository(Poll::class)->find($id);
更新投票数:
$poll->setVotes($poll->getVotes()+1);
坚持到数据库:
$entityManager->坚持($poll);
$entityManager->flush();
或者您也可以使用 ParamConverter to let Symfony get the Poll object for you. More information about updating objects can be found in the Doctrine Guide.
请注意,您的路线将仅匹配现有民意调查,因为 id 是 URL 中的必需参数。您可以添加另一个没有用于创建新投票实体的 ID 的路由。
我是 Symfony 的新手
我正在做一个投票系统,但我想这应该适用于,
目前我的控制器功能是这样的,这只会创建一个带有 1vote 的新行,但不会更新之前创建的任何 $id。
/**
* @Route("/public/{id}/vote", name="poll_vote", methods="GET|POST")
*/
public function vote(Request $request, Poll $poll): Response
{
$inc = 1;
$em = $this->getDoctrine()->getManager();
$entity = new Poll();
$entity->setVotes($inc++);
$em->persist($entity);
$em->flush();
}
return $this->redirectToRoute('poll_public');
}
这是我的 twig 模板按钮
<a href="{{ path('poll_vote', {'id': poll.id}) }}">
这是我的实体
class Poll
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $votes;
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getVotes(): ?int
{
return $this->votes;
}
public function setVotes(?int $votes): self
{
$this->votes = $votes;
return $this;
}
}
我不知道如何从我的实体匹配我的 getID 和从@Route 匹配 $id。
如有任何指导或建议,我们将不胜感激。
谢谢
编辑:
在 Arne 回答后更新了正确的函数:
/**
* @Route("/public/{id}", name="poll_vote", methods="GET|POST")
*/
public function vote($id)
{
$entityManager = $this->getDoctrine()->getManager();
$poll = $entityManager->getRepository(Poll::class)->find($id);
if (!$poll) {
throw $this->createNotFoundException(
'No polls found for id '.$id
);
}
$poll->setVotes($poll->getVotes()+1);
$entityManager->flush();
return $this->redirectToRoute('poll_public', [
'id' => $poll->getId()
]);
}
基本上您必须从您的请求中获取 ID,查询您的投票实体的实体存储库,更新投票并将其保存回您的数据库。
从您的请求中获取 ID
$id = $request->query->get('id');
查询存储库:
$entityManager = $this->getDoctrine()->getManager();
$poll= $entityManager->getRepository(Poll::class)->find($id);
更新投票数:
$poll->setVotes($poll->getVotes()+1);
坚持到数据库:
$entityManager->坚持($poll);
$entityManager->flush();
或者您也可以使用 ParamConverter to let Symfony get the Poll object for you. More information about updating objects can be found in the Doctrine Guide.
请注意,您的路线将仅匹配现有民意调查,因为 id 是 URL 中的必需参数。您可以添加另一个没有用于创建新投票实体的 ID 的路由。