Symfony - 删除相关实体时出错

问题描述 投票:0回答:1

当我想编辑条目时,我遇到了相关实体的问题。我有三个相关的实体:User、Reporting 和 reporting_freigabe。 当我创建新的报告时,我可以选择可以查看我的报告的用户,并且会在 reporting_freigabe 中创建一个状态为打开的条目。 到目前为止这有效。 (Freigabe 类似于评论) 但是,当我编辑报告并在表单中选择其他用户时,reporting_freigabe 中的现有条目应被删除。但是,当我尝试删除时,我收到以下错误:

执行查询时发生异常:SQLSTATE[23000]:违反完整性约束:1452 无法添加或更新子行:外键约束失败 (

bkcwebdev_reporting
.
reporting_freigabe
,CONSTRAINT
FK_9966B42427EE0E60
FOREIGN KEY (
reporting_id) 
)参考文献
reporting
id
)) 我的实体有问题吗?希望有人给我建议

我的报告实体:

#[ORM\Entity(repositoryClass: ReportingRepository::class)]
class Reporting
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 255)]
    private ?string $titel = null;

    #[ORM\Column(type: Types::TEXT, nullable: true)]
    private ?string $beschreibung = null;

    #[ORM\Column(type: Types::TEXT, nullable: true)]
    private ?string $inhalt = null;

    #[ORM\ManyToOne(inversedBy: 'reportingsersteller')]
    #[ORM\JoinColumn(nullable: false)]
    private ?User $ersteller = null;

    #[ORM\Column(length: 255)]
    private ?string $status = null;

    #[ORM\Column]
    private ?\DateTimeImmutable $created_at = null;

    #[ORM\ManyToOne(inversedBy: 'reportings')]
    private ?Kategorie $kategorie = null;

    #[ORM\OneToMany(mappedBy: 'reporting', targetEntity: ReportingFreigabe::class, cascade: ['persist', 'remove'], fetch: 'EAGER')]
    private Collection $reportingFreigabes;

    public function __construct()
    {
        $this->setCreatedAt(new \DateTimeImmutable());
        $this->reportingFreigabes = new ArrayCollection();
    }

    
    // GETTERS SETTERS .....


    /**
     * @return Collection<int, ReportingFreigabe>
     */
    public function getReportingFreigabes(): Collection
    {
        return $this->reportingFreigabes;
    }

    public function addReportingFreigabe(ReportingFreigabe $reportingFreigabe): static
    {
        if (!$this->reportingFreigabes->contains($reportingFreigabe)) {
            $this->reportingFreigabes->add($reportingFreigabe);
            $reportingFreigabe->setReporting($this);
        }

        return $this;
    }

    public function removeReportingFreigabe(ReportingFreigabe $reportingFreigabe): static
    {
        if ($this->reportingFreigabes->removeElement($reportingFreigabe)) {
            // set the owning side to null (unless already changed)
            if ($reportingFreigabe->getReporting() === $this) {
                $reportingFreigabe->setReporting(null);
            }
        }

        return $this;
    }
}

我的“freigabes”的实体

<?php

namespace App\Entity;

use App\Repository\ReportingFreigabeRepository;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Query\AST\Functions\FunctionNode;

#[ORM\Entity(repositoryClass: ReportingFreigabeRepository::class)]
class ReportingFreigabe
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\ManyToOne(inversedBy: 'reportingFreigabes')]
    #[ORM\JoinColumn(nullable: false)]
    private ?Reporting $reporting = null;

    #[ORM\ManyToOne(inversedBy: 'reportingFreigabes', cascade: ['persist', 'remove'], fetch: 'EAGER')]
    #[ORM\JoinColumn(nullable: false)]
    private ?User $benutzer = null;

    #[ORM\Column(length: 255)]
    #[Assert\Choice(choices: ['offen', 'erledigt'])]
    private ?string $status = null;

    public function __construct()
    {
        $this->setStatus('offen');
    }

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

    public function getReporting(): ?Reporting
    {
        return $this->reporting;
    }

    public function setReporting(?Reporting $reporting): static
    {
        $this->reporting = $reporting;

        return $this;
    }

    public function getBenutzer(): ?User
    {
        return $this->benutzer;
    }

    public function setBenutzer(?User $benutzer): static
    {
        $this->benutzer = $benutzer;

        return $this;
    }

    public function getStatus(): ?string
    {
        return $this->status;
    }

    public function setStatus(string $status): static
    {
        $this->status = $status;

        return $this;
    }
}

在报告控制器中,我检查一个数组并将“freigabes”与

相关
$reporting->removeReportingFreigabe($buffFreigabe);

我检查了不同的关系

php symfony
1个回答
0
投票

您从数据库中检索错误,但不是从 symfony 应用程序中检索错误(更准确地说是原则)。

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (bkcwebdev_reporting.reporting_freigabe, CONSTRAINT FK_9966B42427EE0E60 FOREIGN KEY (reporting_id) REFERENCES reporting (id))

通过这个错误,MySQL 说 - 你不能更改(删除)表

reporting_freigabe
中的行,因为该行包含对
reporting
表的引用,并且未提供选项
ON DELETE / ON UPDATE
,因此 MySQL 不知道该怎么做
reporting
表中有行。

Doctrine,默认情况下(架构更新或迁移)不添加选项

ON DELETE / ON UPDATE
。因此,您应该修改迁移(或手动更新架构)并添加此选项,这对 MySQL 来说是如何处理关系的。

以前,使用

SHOW CREATE TABLE reporting_freigabe
查看您的架构。之后,您可以修改外键并添加正确的
ON DELETE / ON UPDATE
选项。

有关约束(外键)的更多信息,请参阅 https://dev.mysql.com/doc/refman/8.0/en/create-table-foreign-keys.html

© www.soinside.com 2019 - 2024. All rights reserved.