I am developing a Symfony 7.1 application. This is my User class (part of it):
<?php
namespace App\Entity;
use App\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
#[ORM\Entity(repositoryClass: UserRepository::class)]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 180, unique: true)]
#[Assert\Email(
message: 'Die Email {{ value }} ist nicht valide.',
)]
private ?string $email = null;
#[ORM\Column]
private array $roles = [];
/**
* @var string The hashed password
*/
#[ORM\Column]
private ?string $password = null;
#[ORM\Column(length: 255)]
#[Assert\NotNull]
private ?string $firstname = null;
#[ORM\Column(length: 255)]
#[Assert\NotNull]
private ?string $lastname = null;
#[ORM\ManyToOne(inversedBy: 'users')]
#[ORM\JoinColumn(nullable: false)]
private ?Club $club = null;
#[ORM\ManyToOne(inversedBy: 'users')]
private ?Squad $squad = null;
#[ORM\Column]
private ?bool $assistance = null;
#[ORM\Column(type: 'boolean')]
private $isVerified = false;
/**
* @var Collection<int, Group>
*/
#[ORM\ManyToMany(targetEntity: Group::class, inversedBy: 'users')]
private Collection $groups;
In my delete-controller I check if there is any ForeignKeyContraint:
catch (ForeignKeyConstraintViolationException $e){
$this->addFlash(
'error',
'Der Benutzer ' . $user->getLastname() . ' '. $user->getFirstname() .' konnte nicht gelöscht werden. Grund: Fremdschlüsselbeziehung!'
);
}
Unfortunately, the exception is not thrown. Does anybody have an idea why? In some cases, the validation works, but here not.