Symfony 5 - 查找(元素)在删除功能(CRUD)中不起作用(?)
Symfony 5 - find(ELEMENT) doesn't work (?) in delete function (CRUD)
我尝试在 Symfony 中创建我的第一个 API。我的删除功能有点问题。
我的实体class:
<?php
namespace App\Entity;
use App\Repository\InFlowsRepository;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=InFlowsRepository::class)
*/
class InFlows
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
public int $id;
/**
* @ORM\Column(type="string", length=255)
*/
public string $inFlowName;
/**
* @ORM\Column(type="float")
*/
public float $inFlowValue;
/**
* @ORM\Column(type="string")
*/
public String $inFlowsDate;
public function getId(): ?int
{
return $this->id;
}
public function getInFlowName(): ?string
{
return $this->inFlowName;
}
public function setInFlowName(string $inFlowName): self
{
$this->inFlowName = $inFlowName;
return $this;
}
public function getInFlowValue(): ?float
{
return $this->inFlowValue;
}
public function setInFlowValue(float $inFlowValue): self
{
$this->inFlowValue = $inFlowValue;
return $this;
}
public function getInFlowsDate(): ?String
{
return $this->inFlowsDate;
}
public function setInFlowsDate(String $inFlowsDate): self
{
$this->inFlowsDate = $inFlowsDate;
return $this;
}
}
还有我的删除控制器:
/**
* @Route("inflows/delete/", name="delete_inflow")
* @throws Exception
*/
public function inFlowDelete(Request $id): JsonResponse {
try {
$repo = $this->getDoctrine()->getManager();
$inflows = $repo->getRepository(InFlows::class)->find($id);
if (!$inflows) {
throw new \JsonException("There is no data to delete!");
}
} catch (Exception $e) {
return new JsonResponse(["data"=>$e->getMessage()]);
}
$repo->remove($inflows);
$repo->flush();
return new JsonResponse("Success!");
}
当我 运行 我的脚本出现错误时:
An exception occurred while executing \u0027SELECT t0.id AS id_1, t0.in_flow_name AS in_flow_name_2, t0.in_flow_value AS in_flow_value_3, t0.in_flows_date AS in_flows_date_4 FROM in_flows t0 WHERE t0.id = ?\u0027 with params [{\u0022attributes\u0022:{},\u0022request\u0022:{},\u0022query\u0022:{},\u0022server\u0022:{},\u0022files\u0022:{},\u0022cookies\u0022:{},\u0022headers\u0022:{}}]:\n\nSQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for type integer: \u0022DELETE \/inflows\/delete\/?id=1 HTTP\/1.1\r\nAccept: *\/*\r\nAccept-Encoding: gzip, deflate, br\r\nCache-Control: no-cache\r\nConnection: keep-alive\r\nContent-Length: \r\nContent-Type: \r\nHost: 127.0.0.1:8000\r\nMod-Rewrite: On\r\nPostman-Token: 6f77209a-8bad-4109-93a8-4c43647d7849\r\nUser-Agent: PostmanRuntime\/7.28.0\r\nX-Php-Ob-Level: 1\r\n\r\n\u0022e2
我不知道为什么我的指令是“t0.id =?”好像。
为什么我的“find($id)”函数不起作用?
有办法解决吗?
感谢您的回复。
/**
* @Route("inflows/delete/{id}", name="delete_inflow")
*/
public function inFlowDelete(InFlows $inFlows): JsonResponse {
$em = $this->getDoctrine()->getManager();
$em->remove($inFlows);
$em->flush();
return new JsonResponse("Success!");
}
我认为问题在于你没有使用 persist ,当你对数据库进行操作时,你必须坚持你的更改,所以它看起来像
/**
* @Route("inflows/delete/", name="delete_inflow")
* @throws 异常
*/
public 函数 inFlowDelete(Request $id): JsonResponse {
try {
$repo = $this->getDoctrine()->getManager();
$inflows = $repo->getRepository(InFlows::class)->find($id);
if (!$inflows) {
throw new \JsonException("There is no data to delete!");
}
} catch (Exception $e) {
return new JsonResponse(["data"=>$e->getMessage()]);
}
$repo->remove($inflows);
$repo->persist();
$repo->flush();
return new JsonResponse("Success!");
}
如果我可以给你一个建议,不要使用 remove(),因为它会从物理上删除你的行,有时最好从逻辑上删除,所以使用 setDeletedAt(new \Datetime());
所以我删除了我的 table 并创建了新的迁移文件。我的代码仍然与我在@nikoshr 解决方案中发布的代码相同。它有效!很奇怪,但正如他们所说 - 灯笼下最黑暗。
我想我知道出了什么问题,这里你输入的是一个包含很多信息的请求(httpd 请求)你可以在你的浏览器中检查它,无论如何最好的解决方案是将 id 作为参数传递并且像这样删除它:
/**
* @Route("inflows/delete/{id}", name="delete_inflow")
* @throws 异常
*/
public 函数 inFlowDelete(int $id): JsonResponse {
try {
$repo = $this->getDoctrine()->getManager();
$inflows = $repo->getRepository(InFlows::class)->find($id);
if (!$inflows) {
throw new \JsonException("There is no data to delete!");
}
} catch (Exception $e) {
return new JsonResponse(["data"=>$e->getMessage()]);
}
$repo->remove($inflows);
$repo->flush();
return new JsonResponse("Success!");
}
试试看,让我知道最新消息!
我尝试在 Symfony 中创建我的第一个 API。我的删除功能有点问题。
我的实体class:
<?php
namespace App\Entity;
use App\Repository\InFlowsRepository;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=InFlowsRepository::class)
*/
class InFlows
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
public int $id;
/**
* @ORM\Column(type="string", length=255)
*/
public string $inFlowName;
/**
* @ORM\Column(type="float")
*/
public float $inFlowValue;
/**
* @ORM\Column(type="string")
*/
public String $inFlowsDate;
public function getId(): ?int
{
return $this->id;
}
public function getInFlowName(): ?string
{
return $this->inFlowName;
}
public function setInFlowName(string $inFlowName): self
{
$this->inFlowName = $inFlowName;
return $this;
}
public function getInFlowValue(): ?float
{
return $this->inFlowValue;
}
public function setInFlowValue(float $inFlowValue): self
{
$this->inFlowValue = $inFlowValue;
return $this;
}
public function getInFlowsDate(): ?String
{
return $this->inFlowsDate;
}
public function setInFlowsDate(String $inFlowsDate): self
{
$this->inFlowsDate = $inFlowsDate;
return $this;
}
}
还有我的删除控制器:
/**
* @Route("inflows/delete/", name="delete_inflow")
* @throws Exception
*/
public function inFlowDelete(Request $id): JsonResponse {
try {
$repo = $this->getDoctrine()->getManager();
$inflows = $repo->getRepository(InFlows::class)->find($id);
if (!$inflows) {
throw new \JsonException("There is no data to delete!");
}
} catch (Exception $e) {
return new JsonResponse(["data"=>$e->getMessage()]);
}
$repo->remove($inflows);
$repo->flush();
return new JsonResponse("Success!");
}
当我 运行 我的脚本出现错误时:
An exception occurred while executing \u0027SELECT t0.id AS id_1, t0.in_flow_name AS in_flow_name_2, t0.in_flow_value AS in_flow_value_3, t0.in_flows_date AS in_flows_date_4 FROM in_flows t0 WHERE t0.id = ?\u0027 with params [{\u0022attributes\u0022:{},\u0022request\u0022:{},\u0022query\u0022:{},\u0022server\u0022:{},\u0022files\u0022:{},\u0022cookies\u0022:{},\u0022headers\u0022:{}}]:\n\nSQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for type integer: \u0022DELETE \/inflows\/delete\/?id=1 HTTP\/1.1\r\nAccept: *\/*\r\nAccept-Encoding: gzip, deflate, br\r\nCache-Control: no-cache\r\nConnection: keep-alive\r\nContent-Length: \r\nContent-Type: \r\nHost: 127.0.0.1:8000\r\nMod-Rewrite: On\r\nPostman-Token: 6f77209a-8bad-4109-93a8-4c43647d7849\r\nUser-Agent: PostmanRuntime\/7.28.0\r\nX-Php-Ob-Level: 1\r\n\r\n\u0022e2
我不知道为什么我的指令是“t0.id =?”好像。 为什么我的“find($id)”函数不起作用? 有办法解决吗?
感谢您的回复。
/**
* @Route("inflows/delete/{id}", name="delete_inflow")
*/
public function inFlowDelete(InFlows $inFlows): JsonResponse {
$em = $this->getDoctrine()->getManager();
$em->remove($inFlows);
$em->flush();
return new JsonResponse("Success!");
}
我认为问题在于你没有使用 persist ,当你对数据库进行操作时,你必须坚持你的更改,所以它看起来像 /** * @Route("inflows/delete/", name="delete_inflow") * @throws 异常 */ public 函数 inFlowDelete(Request $id): JsonResponse {
try {
$repo = $this->getDoctrine()->getManager();
$inflows = $repo->getRepository(InFlows::class)->find($id);
if (!$inflows) {
throw new \JsonException("There is no data to delete!");
}
} catch (Exception $e) {
return new JsonResponse(["data"=>$e->getMessage()]);
}
$repo->remove($inflows);
$repo->persist();
$repo->flush();
return new JsonResponse("Success!");
}
如果我可以给你一个建议,不要使用 remove(),因为它会从物理上删除你的行,有时最好从逻辑上删除,所以使用 setDeletedAt(new \Datetime());
所以我删除了我的 table 并创建了新的迁移文件。我的代码仍然与我在@nikoshr 解决方案中发布的代码相同。它有效!很奇怪,但正如他们所说 - 灯笼下最黑暗。
我想我知道出了什么问题,这里你输入的是一个包含很多信息的请求(httpd 请求)你可以在你的浏览器中检查它,无论如何最好的解决方案是将 id 作为参数传递并且像这样删除它:
/**
* @Route("inflows/delete/{id}", name="delete_inflow")
* @throws 异常
*/
public 函数 inFlowDelete(int $id): JsonResponse {
try {
$repo = $this->getDoctrine()->getManager();
$inflows = $repo->getRepository(InFlows::class)->find($id);
if (!$inflows) {
throw new \JsonException("There is no data to delete!");
}
} catch (Exception $e) {
return new JsonResponse(["data"=>$e->getMessage()]);
}
$repo->remove($inflows);
$repo->flush();
return new JsonResponse("Success!");
}
试试看,让我知道最新消息!