<?php
use DateTime;
use Doctrine\ORM\Mapping as A;
use Exception;
/**
* @A\Entity
* @A\Table(name="test")
* @A\EntityListeners(value="TestListener")
*/
class Test
{
public function __construct(string $name)
{
$this->name = $name;
$this->creationDate = new DateTime();
}
/**
* @A\Column(type="integer", name="id")
* @A\Id
* @A\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @A\Column(type="datetime", name="creation_date", nullable=true)
*/
private $creationDate;
/**
* @A\Column(type="string", name="name", length=255)
*/
private $name;
}
class TestListener
{
public function prePersist(Test $test)
{
dump($test);
throw new Exception('I don\'t want to persist for this snippet');
}
}
$test = new Test('name');
try {
$entityManager->persist($test);
$entityManager->flush();
} catch (Exception $e) {
echo $e->getMessage() . PHP_EOL;
}