我使用 symfony 3.1。我想在我的 symfony 应用程序中创建一个通知系统。我创建了一个名为通知的实体来保存通知。因此,当用户在数据库中创建、编辑或删除记录时,我想将此操作保存在通知表中。我使用了 HasLifecycleCallbacks() 注释方法,它迫使我在我的实体中创建一个控制器对象,但没有任何效果。我该怎么做?还有其他解决方案吗?
/**
* @ORM\Table(name="user")
* @ORM\Entity(repositoryClass="CM\UserBundle\Repository\UserRepository")
* @ORM\HasLifecycleCallbacks()
*/
class User extends BaseUser {
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
*
* @ORM\Column(name="nom", type="string", unique=true, length=255, nullable=true)
* @Assert\NotBlank()
*/
protected $nom;
/**
* @var int
*
* @ORM\Column(name="numero", type="integer", unique=true, nullable=true)
* @Assert\NotBlank()
*/
protected $numero;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set nom
*
* @param string $nom
*
* @return User
*/
public function setNom($nom)
{
$this->nom = $nom;
return $this;
}
/**
* Get nom
*
* @return string
*/
public function getNom()
{
return $this->nom;
}
/**
* Set numero
*
* @param integer $numero
*
* @return User
*/
public function setNumero($numero)
{
$this->numero = $numero;
return $this;
}
/**
* Get numero
*
* @return int
*/
public function getNumero()
{
return $this->numero;
}
/**
* @ORM\PreRemove
*/
public function notify(){
$controlleur = new RemoveController();
$em = $controlleur->getDoctrine()->getManager();
$notif = new Notification();
$notif->setOperation('recording');
$notif->setUser('William');
$em->persist($notif);
$em->flush();
}
}