Php Symfony 5-查找(元素)不';删除功能(CRUD)中的t工作(?)

Php Symfony 5-查找(元素)不';删除功能(CRUD)中的t工作(?),php,symfony,Php,Symfony,我尝试在Symfony中创建我的第一个API。我的删除功能有点问题 我的实体类: <?php namespace App\Entity; use App\Repository\InFlowsRepository; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity(repositoryClass=InFlowsRepository::class) */ class InFlows { /** * @ORM\Id

我尝试在Symfony中创建我的第一个API。我的删除功能有点问题

我的实体类:

<?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;
    }
}
当我运行脚本时,我得到一个错误:

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
我不知道为什么我的指令“where t0.id=?”看起来像这样。 为什么我的“查找($id)”功能不起作用? 有办法解决吗


感谢您的回复。

我认为问题在于您没有使用persist,当您对数据库进行操作时,您必须将您的更改持久化,以便看起来像
/**
 * @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!");
}
/** *@Route(“流入/删除/”,name=“删除流入”) *@抛出异常 */ 公共函数inFlowDelete(请求$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!");
}
    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!");
}

如果我能给你一个建议,不要使用remove(),因为它会从物理上删除你的行,有时最好从逻辑上删除,所以使用setDeletedAt(new\Datetime())所以我删除了我的表并创建了新的迁移文件。我的代码仍然和我在@nikoshr solution中发布的代码一样。它仍然有效!非常奇怪,但正如他们所说的-灯下最暗。

我想我知道问题出在哪里了,这里您的输入是一个请求(httpd请求)其中包含大量信息您可以在浏览器中检查此项,因此最好的解决方案是将id作为参数传递,并按如下方式删除它:
/** *@Route(“流入/删除/{id}”,name=“删除流入”) *@抛出异常 */ 公共函数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->persist();
    $repo->flush();

    return new JsonResponse("Success!");
}
    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!");
}

试试看,让我随时更新!

你将一个
请求
对象传递给
查找
。这可能不是你想要做的。@nikoshr但是
请求
对象是一个元素(id)来自用户。我将此元素传递给我的find函数,其中的结果是
对象
。此对象是要删除函数的参数。不正确?我无法传递重构删除函数,但现在当我传递find(['id'=>$id]时,得到另一个错误:
无法将null分配给属性App\Entity\InFlows:$id类型int(500内部服务器错误)
它不起作用-我遇到这个错误:
无法为属性App\Entity\influences::$id类型int
您应该为实体中的
id
属性添加
null
。例如
public?int$id=null;
。因为原则在删除后为id设置null。感谢您的分享。不幸的是,您的解决方案n不起作用。我仍然有和以前一样的错误。您的解决方案与我在代码中实现的解决方案相同,而且效果非常好。我不知道为什么我要在函数arg中添加请求ID。谢谢!