我正在尝试构建一个 preUpdate,我正在使用 postUpdate 进行此操作,但我没有考虑到这样一个事实,即无论何时对该实体进行编辑,无论字段如何,我都会进行更改...所以我读到我应该改用 preUpdate ...
这是我的更新前监听器:
namespace Prizes\PrizesBundle\EventListener;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Prizes\PrizesBundle\Entity\Prize;
use Prizes\CatalogBundle\Entity\CatalogHasPrize as CHP;
/**
* Description of DeactivatePrizesInCatalog
*
* @author Victoria Noguera
* Desactiva premios en catalogos cuando se desactiven en el prize central.
*/
/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
*/
class DeactivatePrizesInCatalog {
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpdate(LifecycleEventArgs $args) {
$entity = $args->getEntity();
$entityManager = $args->getEntity
Manager();
if ($entity instanceof Prize) {
if ($args->hasChangedField('status')) {
$status_new = $args->getNewValue('status');
if ($status_new->getName() == "Inactive") {
$statusinactive = $entityManager->getRepository('AppStatusBundle:Status')->find(8);
$q = $entityManager->createQuery("UPDATE CatalogBundle:CatalogHasPrize c SET c.status = :statusid WHERE c.status = 7 AND c.prize = :prizeid")
->setParameters(array('statusid' => $statusinactive, 'prizeid' => $entity->getId()));
$rs = $q->getResult();
} else if ($status_new->getName() == "Active") {
$statusinactive = $entityManager->getRepository('AppStatusBundle:Status')->find(7);
$q = $entityManager->createQuery("UPDATE CatalogBundle:CatalogHasPrize c SET c.status = :statusid WHERE c.status = 8 AND c.prize = :prizeid")
->setParameters(array('statusid' => $statusinactive, 'prizeid' => $entity->getId()));
$rs = $q->getResult();
}
}
}
}
}
这就是我在 config.yml 文件中执行的方式(我只是在 services.yml 中读到它,但我稍后会更改它)
services:
preupdate.listener:
class: Prizes\PrizesBundle\EventListener\DeactivatePrizesInCatalog
tags:
- { name: doctrine.event_listener , event: preUpdate }
但是,当我这样做时,我的项目从功能齐全变为 503 服务不可用。即使我只是写public function preUpdate(LifecycleEventArgs $args) { }
它崩溃,我假设一个配置问题,但我不知道我做错了什么。
我将不胜感激提供的任何帮助。谢谢