Skip to content

Commit

Permalink
Create EntityListener
Browse files Browse the repository at this point in the history
  • Loading branch information
KoukiFactori committed Feb 15, 2023
1 parent 9c46082 commit 7b7a93e
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/EntityListener/RatingSetUserListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace App\EntityListener;

use App\Entity\Rating;
use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener;
use Doctrine\ORM\Events;
use Symfony\Component\Security\Core\Security;

#[AsEntityListener(
event: Events::prePersist,
entity: Rating::class
)]
class RatingSetUserListener
{
private Security $security;

public function __construct(Security $security)
{
$this->security = $security;
}

public function prePersist(Rating $rating)
{
if (null === $rating->getUser()) {
$rating->setUser($this->security->getUser());
}
}
}

0 comments on commit 7b7a93e

Please sign in to comment.