UniqueEntity -Attempted to load class "Constraint" from namespace "Symfony\Component\Validator" #53379
Unanswered
EtienneVformateur
asked this question in
Q&A
Replies: 2 comments 1 reply
-
You need to have |
Beta Was this translation helpful? Give feedback.
0 replies
-
Thanks , I don't get the error anymore but it's not working. I can add Follow using this : public function add(EntityManagerInterface $em, User $user): JsonResponse
{
try {
$follow = new Follow();
$follower = $this->getUser();
$follow->setFollower($follower);
$follow->setFollowing($user);
$follow->setCreatedAt(new \DateTimeImmutable());
$em->persist($follow);
$em->flush();
return new JsonResponse(['status' => 'success', 'message' => 'Followed successfully'], 200);
} catch (\Exception $e) {
return new JsonResponse(['status' => 'error', 'message' => $e->getMessage()], 500);
}
} <?php
namespace App\Entity;
use App\Repository\FollowRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
#[UniqueEntity(fields: ['follower', 'following'], message: 'You are already following this user')]
#[ORM\Entity(repositoryClass: FollowRepository::class)]
class Follow
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\ManyToOne]
#[ORM\JoinColumn(nullable: false)]
private ?User $follower = null;
#[ORM\Column]
private ?\DateTimeImmutable $createdAt = null;
#[ORM\ManyToOne]
#[ORM\JoinColumn(nullable: false)]
private ?User $following = null;
public function getId(): ?int
{
return $this->id;
}
public function getFollower(): ?User
{
return $this->follower;
}
public function setFollower(?User $follower): static
{
$this->follower = $follower;
return $this;
}
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeImmutable $createdAt): static
{
$this->createdAt = $createdAt;
return $this;
}
public function getFollowing(): ?User
{
return $this->following;
}
public function setFollowing(?User $following): static
{
$this->following = $following;
return $this;
}
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
{{title}}
{{editor}}'s edit
{{editor}}'s edit
-
Symfony version(s) affected
7.0
Description
I followed the documentation. to add UniqueEntity : https://symfony.com/doc/current/reference/constraints/UniqueEntity.html#em
Here is my entity :
My composer.json :
How to reproduce
Possible Solution
No response
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions