是的,你可以,没有记录你是对的,我会确保它很快。
这里的集成测试:https ://github.com/graphaware/neo4j-php-ogm/blob/master/tests/Integration/EventListenerIntegrationTest.php
首先,您需要创建一个类,它将充当preFlush
EntityManager 事件的 EventListener 和一个对事件作出反应的方法:
<?php
namespace GraphAware\Neo4j\OGM\Tests\Integration\Listeners;
use GraphAware\Neo4j\OGM\Event\PreFlushEventArgs;
use GraphAware\Neo4j\OGM\Tests\Integration\Model\User;
class Timestamp
{
public function preFlush(PreFlushEventArgs $eventArgs)
{
$dt = new \DateTime("NOW", new \DateTimeZone("UTC"));
foreach ($eventArgs->getEntityManager()->getUnitOfWork()->getNodesScheduledForCreate() as $entity) {
if ($entity instanceof User) {
$entity->setUpdatedAt($dt);
}
}
}
}
然后您可以在创建实体管理器后注册此事件侦听器:
/**
* @group pre-flush
*/
public function testPreFlushEvent()
{
$this->clearDb();
$this->em->getEventManager()->addEventListener(Events::PRE_FLUSH, new Timestamp());
$user = new User("ikwattro");
$this->em->persist($user);
$this->em->flush();
$this->assertNotNull($user->getUpdatedAt());
var_dump($user->getUpdatedAt());
}
测试结果:
ikwattro@graphaware-team ~/d/g/p/ogm> ./vendor/bin/phpunit tests/ --group pre-flush
PHPUnit 5.6.2 by Sebastian Bergmann and contributors.
Runtime: PHP 5.6.27
Configuration: /Users/ikwattro/dev/graphaware/php/ogm/phpunit.xml.dist
. 1 / 1 (100%)int(1486763241)
Time: 378 ms, Memory: 5.00MB
OK (1 test, 1 assertion)
数据库中的结果: