Symfony – set expiration time automatically

  doctrine, getter-setter, php, symfony, symfony4

I am setting new entity in my form and I have field ‘created’ which I want to use to set expiration time automatically. I want for expired field to be set on true 30 minutes after entity is set.

I have some logic created but I think it won’t work.

My part of the code:

 $dateNow = new DateTime();
    $entity->setCreated($dateNow)->modify("+33 Minutes");
    if($dateNow >= $entity){
        $entity->setExpired(true);
        $this->em->persist($entity);
        $this->em->flush();
    }

if($entity->isExpired()) {
        throw new /Exception('Sorry,it is expired.');
    }

Do you have any idea how to do this when using setter in Doctrine? Thanks

Source: Symfony Questions

LEAVE A COMMENT