我对doctrine2关系和事件有疑问。
我有这样的关系:
点:
My\Entity\Point:
type: entity
table: null
manyToOne:
user:
targetEntity: User
game:
targetEntity: Game
fields:
id:
type: integer
id: true
generator:
strategy: AUTO
point:
type: integer
date:
type: datetime
lifecycleCallbacks: { }
游戏:
My\Entity\Game:
type: entity
table: null
manyToOne:
user:
targetEntity: User
oneToMany:
points:
targetEntity: Point
mappedBy: game
fields:
id:
type: integer
id: true
generator:
strategy: AUTO
date:
type: datetime
win:
type: boolean
lifecycleCallbacks: { }
这样:
另外,我有一些eventlistener
/**
* @param LifecycleEventArgs $args
*/
public function preUpdate(LifecycleEventArgs $args) {
$entity = $args->getEntity();
if ($entity instanceof Game && $args->hasChangedField('win')) {
$this->point->setUser($entity->getUser());
$this->point->setGame($entity);
switch(true){
case $args->getNewValue('win'):
$this->point->setPoint(1);
break;
case $args->getOldValue('win'):
$this->point->setPoint(-1);
break;
default:
break;
}
$this->haveToBeFlush = true;
}
}
/**
* @param PostFlushEventArgs $eventArgs
*/
public function postFlush(PostFlushEventArgs $eventArgs)
{
if ($this->haveToBeFlush === true) {
$eventArgs->getEntityManager()->persist($this->point);
$eventArgs->getEntityManager()->flush();
}
}
在postFlush之后,我可以在db中看到这样的记录:
id;user_id;point;date;game_id
6;17;-1;2014-03-26 15:02:01;NULL
我不知道为什么game_id出现空值
答案 0 :(得分:0)
好的,现在一切都清楚了。
第一时刻,我虽然在我与实体合作时会出现这个问题,我为此创建了事件监听器" postUpdate"。我错了。
问题在于缓存。清除缓存并重新启动Apache后,一切正常。