0

我使用 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();   
}
}
4

1 回答 1

1

我已经解决了我的问题。我没有很好地阅读文档。然后我做了更多的研究。所以这是一个适用于 symfony 3.1 的解决方案。我使用了依赖注入。我已经注入 ManagerRegistry 以使实体管理器服务和 TokenStorage 用于 tokenStorage 服务以了解当前连接的用户。我创建了一个文件夹名称NotificationDB。然后我还创建了一个类名NotificationDB.php,这是代码。对于这个例子,我假设我有一个表单来注册一个foo实体。

namespace CM\GestionBundle\NotificationBD;
use Doctrine\Common\Persistence\ManagerRegistry;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; 
use \CM\GestionBundle\Entity\Notification;    

class NotificationBD {  

private $managerRegistry;

/**
 * @var TokenStorage
 */
private $tokenStorage;

public function __construct(ManagerRegistry $managerRegistry, TokenStorage $tokenStorage)
{
    $this->managerRegistry = $managerRegistry;
    $this->tokenStorage = $tokenStorage;
}
public function notifyEvent(){
    $entityManager = $this->managerRegistry->getManager();
    $user = $this->tokenStorage->getToken()->getUser();
    $notif = new Notification();
    $notif->setDateNotif(new \Datetime());
    $notif->setUser($user);
    $notif->setActionNotif('recording');
    $notif->setObjetNotif('foo');
    $entityManager->persist($notifEdition);
    $entityManager->flush();
}
}

CM\GestionBundle\Resources\config** 中,我已在 **services.yml文件中配置为服务,这是内容:

    CM_gestion.notification:
        class: CM\GestionBundle\NotificationBD\NotificationBD
        arguments: ['@doctrine', '@security.token_storage']

然后我创建了一个监听器,它会调用这个服务。对于此示例,我将创建一个foo 实体侦听器,它将侦听注册事件并使用该服务来持久化通知实体。这是我的foo 听众

namespace CM\GestionBundle\DoctrineListener;
use CM\GestionBundle\NotificationBD\NotificationBD;
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
use \CM\GestionBundle\Entity\Foo;

class FooListener {

/**
* @var NotificationBD
*/
private $notificationBD;

public function __construct(NotificationBD $notificationBD) {
    $this->notificationBD = $notificationBD;
}

public function postPersist(LifecycleEventArgs $args) {
    $entity = $args->getObject();

    if (!$entity instanceof Foo) {
        return;
    }
    $this->notificationBD->notifyEvent();
}
}

CM\GestionBundle\Resources\config 最后要做的就是在services.yml文件中添加这个监听器。这里是内容

CM_gestion.doctrine_listener.foo:
    class:  CM\GestionBundle\DoctrineListener\FooListener
    arguments:
        - "@CM_gestion.notification"
    tags:
        - {name: doctrine.event_listener, event: postPersist}

就这些。这个解决方案在 symfony 3.1 中对我有用。此问题可以标记为已解决。感谢

于 2016-08-03T10:55:48.480 回答