我尝试对 Doctrine2 使用 Gedmo Loggable 扩展,但出现错误,我不明白为什么。
我得到了错误
致命错误:未捕获的 Doctrine\ORM\Mapping\MappingException:“Gedmo\Loggable\Entity\MappedSuperclass\AbstractLogEntry”的类“Gedmo\Loggable\Entity\LogEntry”子类不是有效的实体或映射的超类。在 C:\Projects\easyprod\librairies\doctrine\orm\lib\Doctrine\ORM\Mapping\MappingException.php 行 340
当我跑步时
$article = new \classeDoctrine\Article;
$article->setTitle('my title');
$oGestionnaireEntites->persist($article);
$oGestionnaireEntites->flush();
$article = $oGestionnaireEntites->find('\classeDoctrine\Article', 1);
$article->setTitle('my new title');
$oGestionnaireEntites->persist($article);
$oGestionnaireEntites->flush();
//$repo = $em->getRepository('Gedmo\Loggable\Entity\LogEntry');
$repo = $oGestionnaireEntites->getRepository('Gedmo\Loggable\Entity\LogEntry'); // we use default log entry class
$article = $oGestionnaireEntites->find('classeDoctrine\Article', 1);
$logs = $repo->getLogEntries($article);
// lets revert to first version
$repo->revert($article, 1);
// notice article is not persisted yet, you need to persist and flush it
echo $article->getTitle(); // prints "my title"
$oGestionnaireEntites->persist($article);
$oGestionnaireEntites->flush();
配置文件——
use Doctrine\ORM\EntityManager,
Doctrine\ORM\Configuration,
Doctrine\ORM\Tools\Setup,
Doctrine\ORM\Proxy\Autoloader;
use Doctrine\Common\Annotations\AnnotationRegistry;
//charge l'autoload voir composer.json
require_once ROOT . "/librairies/autoload.php";
// First of all autoloading of vendors
$loader = require ROOT . "/librairies/autoload.php";
// gedmo extensions
$loader->add('Gedmo', ROOT . "/librairies/gedmo/doctrine-extensions/lib");
$tCheminEntites = array(
ROOT . '/class/doctrine',
ROOT . '/class/doctrine/entite/grilleResultat',
ROOT . '/class/doctrine/entite/analyse',
ROOT . '/class/doctrine/entite/matrice',
ROOT . '/class/doctrine/entite/test',
);
foreach ($tCheminEntites as $sCheminEntite) {
// autoloader for Entity namespace
$loader->add('classeDoctrine', $sCheminEntite);
}
//Paramètre de la base de données
$tBddParametres = array(
'driver' => 'pdo_mysql',
'user' => 'root',
'password' => '',
'dbname' => 'easyprod',
'charset' => 'UTF8'
);
Doctrine\Common\Annotations\AnnotationRegistry::registerFile(
ROOT . '/librairies/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php'
);
$bModeDeveloppement = true;
// globally used cache driver, in production use APC or memcached
$cache = new Doctrine\Common\Cache\ArrayCache;
// standard annotation reader
$annotationReader = new Doctrine\Common\Annotations\SimpleAnnotationReader();
$annotationReader->addNamespace('Doctrine\ORM\Mapping');
$cachedAnnotationReader = new Doctrine\Common\Annotations\CachedReader(
$annotationReader, // use reader
$cache // and a cache driver
);
// create a driver chain for metadata reading
$driverChain = new Doctrine\ORM\Mapping\Driver\DriverChain();
// load superclass metadata mapping only, into driver chain
// also registers Gedmo annotations.NOTE: you can personalize it
Gedmo\DoctrineExtensions::registerAbstractMappingIntoDriverChainORM(
$driverChain, // our metadata driver chain, to hook into
$cachedAnnotationReader // our cached annotation reader
);
// now we want to register our application entities,
// for that we need another metadata driver used for Entity namespace
$annotationDriver = new Doctrine\ORM\Mapping\Driver\AnnotationDriver(
$cachedAnnotationReader, // our cached annotation reader
$tCheminEntites// paths to look in
);
// NOTE: driver for application Entity can be different, Yaml, Xml or whatever
// register annotation driver for our application Entity namespace
$driverChain->addDriver($annotationDriver, 'classeDoctrine');
//$oDefaultAnnotationDriver = $config->newDefaultAnnotationDriver($tCheminEntites);
// NOTE: driver for application Entity can be different, Yaml, Xml or whatever
// register annotation driver for our application Entity fully qualified namespace
//$driverChain->addDriver($oDefaultAnnotationDriver, 'classeDoctrine');
// general ORM configuration
$config = new Doctrine\ORM\Configuration;
$config->setProxyDir(sys_get_temp_dir());
$config->setProxyNamespace('Proxy');
$config->setAutoGenerateProxyClasses(false); // this can be based on production config.
// register metadata driver
$config->setMetadataDriverImpl($driverChain);
// use our already initialized cache driver
$config->setMetadataCacheImpl($cache);
$config->setQueryCacheImpl($cache);
// create event manager and hook preferred extension listeners
$evm = new Doctrine\Common\EventManager();
// gedmo extension listeners, remove which are not used
// loggable, not used in example
$loggableListener = new Gedmo\Loggable\LoggableListener;
$loggableListener->setAnnotationReader($cachedAnnotationReader);
$evm->addEventSubscriber($loggableListener);
//Paramètre de la base de données
$tBddParametres = array(
'driver' => 'pdo_mysql',
'user' => 'root',
'password' => '',
'dbname' => 'easyprod',
'charset' => 'UTF8'
);
$oGestionnaireEntites = EntityManager::create($tBddParametres, $config, $evm);
$oGestionnaireEntites->getConfiguration()->addCustomHydrationMode('HydratorValeurSansNomColonne', 'boiteAOutils\HydratorValeurSansNomColonne');