Doctrine generate entities 命令会产生额外的 src/Gedmo 文件,这会导致命令停止工作,直到删除文件夹
Doctrine generate entities command produces extra src/Gedmo files, which causes command stops working until folder removing
我在独立的 noframework 应用程序中使用 Doctrine 和 Gedmo extension。
自动加载是通过作曲家完成的,composer.json content:
{
"autoload": {
"psr-0": {
"App": "src"
}
},
"require": {
"doctrine/orm": "^2.5",
"gedmo/doctrine-extensions": "^2.4"
}
}
App core 类放在/src目录,composer文件放在/vendor
Doctrine是通过factory配置的,主要代码如下:
<?php
namespace App\Factory;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\Common\Annotations\CachedReader;
use Doctrine\Common\Cache\CacheProvider;
use Doctrine\Common\Cache\FileCache;
use Doctrine\Common\EventManager;
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
class DoctrineFactory implements FactoryInterface
{
/**
* @param ContainerInterface $c
* @return mixed
*/
public function __invoke(ContainerInterface $c)
{
// Set up caches
$cache = new FileCache('runtime/cache/doctrine');
// Annotation reader
$annotationReader = new AnnotationReader;
$cachedAnnotationReader = new CachedReader($annotationReader, $cache);
AnnotationRegistry::registerLoader(array(require 'vendor/autoload.php', 'loadClass'));
// Add Gedmo extensions
$driverChain = new MappingDriverChain();
\Gedmo\DoctrineExtensions::registerAbstractMappingIntoDriverChainORM($driverChain, $cachedAnnotationReader);
// Set up driver to read annotations from entities
$annotationDriver = new AnnotationDriver($cachedAnnotationReader, 'src'));
$driverChain->addDriver($annotationDriver, 'App\Entity');
// General doctrine configuration
$doctrineConfig = new Configuration;
$doctrineConfig->setProxyDir(sys_get_temp_dir()));
$doctrineConfig->setProxyNamespace('App\Entity\Proxy');
$doctrineConfig->setAutoGenerateProxyClasses(false);
$doctrineConfig->setMetadataDriverImpl($driverChain);
$doctrineConfig->setMetadataCacheImpl($cache);
$doctrineConfig->setQueryCacheImpl($cache);
// Event manager to hook extensions
$evm = new EventManager();
// Tree extension
$treeListener = new \Gedmo\Tree\TreeListener;
$treeListener->setAnnotationReader($cachedAnnotationReader);
$evm->addEventSubscriber($treeListener);
// Create EntityManager
// $config['conn'] is connection credentials
return EntityManager::create($config['conn'], $doctrineConfig, $evm);
}
}
我的实体是:
<?php
namespace App\Entity;
use \Doctrine\ORM\Mapping as ORM;
use \Gedmo\Mapping\Annotation as Gedmo;
/**
* Class ProductCategory2
* @package App\Entity
*
* @Gedmo\Tree(type="nested")
* @ORM\Entity(repositoryClass="Gedmo\Tree\Entity\Repository\NestedTreeRepository")
*/
class ProductCategory2
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
*/
private $id;
/**
* @ORM\Column(type="string", length=50)
*/
private $name;
/**
* @Gedmo\TreeLeft
* @ORM\Column(type="integer")
*/
private $lft;
/**
* @Gedmo\TreeLevel
* @ORM\Column(type="integer")
*/
private $lvl;
/**
* @Gedmo\TreeRight
* @ORM\Column(type="integer")
*/
private $rgt;
/**
* @Gedmo\TreeRoot
* @ORM\Column(type="integer", nullable=true)
* @var
*/
private $root;
/**
* @Gedmo\TreeParent
* @ORM\ManyToOne(targetEntity="ProductCategory2", inversedBy="children")
*/
private $parent;
}
我的 cli-config.php 配置正确。
我 运行 doctrine cli 工具通过命令生成实体样板代码:
“vendor/bin/doctrine” orm:generate-entities src
它回答我:
Processing entity
“Gedmo\Translatable\Entity\MappedSuperclass\AbstractPersonal\Translation”
Processing entity
“Gedmo\Translatable\Entity\MappedSuperclass\AbstractTranslation”
Processing entity “Gedmo\Loggable\Entity\MappedSuperclass\AbstractLogEntry”
Processing entity “Gedmo\Tree\Entity\MappedSuperclass\AbstractClosure”
Processing entity “App\Entity\ProductCategory2”
实体工作正常,但命令将额外的文件添加到我的 src 文件夹中:
src\Gedmo
├───Loggable
│ └───Entity
│ └───MappedSuperclass/AbstractLogEntry.php
├───Translatable
│ └───Entity
│ └───MappedSuperclass/AbstractTranslation.php
└───Tree
└───Entity
└───MappedSuperclass/AbstractClosure.php
如果我通过上述命令再次生成实体,则会出现错误。
PHP Fatal error: Cannot redeclare class
Gedmo\Loggable\Entity\MappedSuperclass\AbstractLogEntry in
\src\Gedmo\Loggable\Entity\MappedSuperclass\AbstractLogEntry.php
on line 9
要修复它,我需要先删除 <ROOT>/src/Gedmo
目录。
任何人都可以帮助找到配置中的错误以防止出现这种烦人的额外文件吗?
感谢帮助
我在 doctrine generate-entities 命令后添加了 hack 来清除烦人的目录。 cli-config.php 的完整列表如下:
<?php
use Doctrine\ORM\Tools\Console\ConsoleRunner;
use Interop\Container\ContainerInterface;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
use Symfony\Component\EventDispatcher\EventDispatcher;
require "vendor/autoload.php";
/** @var ContainerInterface $container */
$container = require 'app/bootstrap.php';
$dispatcher = new EventDispatcher();
// Post terminate cli command listener
$dispatcher->addListener(ConsoleEvents::TERMINATE, function(ConsoleTerminateEvent $event) {
$commandName = $event->getCommand()->getName();
switch($commandName) {
case 'orm:generate-entities':
// clear /src/Gedmo dir
\App\Utils\FilesystemUtils::removeDir('src/Gedmo');
break;
}
});
// Create doctrine cli environment via helper
$helperSet = ConsoleRunner::createHelperSet($container->get(\Doctrine\ORM\EntityManager::class));
// Wrap it into Symfony Console App and add some extra commands
$app = new Application('Doctrine Command Line Interface', \Doctrine\ORM\Version::VERSION);
$app->setDispatcher($dispatcher);
$app->setCatchExceptions(true);
$app->setHelperSet($helperSet);
// add default commands
ConsoleRunner::addCommands($app);
// here you may add extra commadts via $app->add(..)
$app->run();
官方文档:
我在独立的 noframework 应用程序中使用 Doctrine 和 Gedmo extension。 自动加载是通过作曲家完成的,composer.json content:
{
"autoload": {
"psr-0": {
"App": "src"
}
},
"require": {
"doctrine/orm": "^2.5",
"gedmo/doctrine-extensions": "^2.4"
}
}
App core 类放在/src目录,composer文件放在/vendor Doctrine是通过factory配置的,主要代码如下:
<?php
namespace App\Factory;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\Common\Annotations\CachedReader;
use Doctrine\Common\Cache\CacheProvider;
use Doctrine\Common\Cache\FileCache;
use Doctrine\Common\EventManager;
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
class DoctrineFactory implements FactoryInterface
{
/**
* @param ContainerInterface $c
* @return mixed
*/
public function __invoke(ContainerInterface $c)
{
// Set up caches
$cache = new FileCache('runtime/cache/doctrine');
// Annotation reader
$annotationReader = new AnnotationReader;
$cachedAnnotationReader = new CachedReader($annotationReader, $cache);
AnnotationRegistry::registerLoader(array(require 'vendor/autoload.php', 'loadClass'));
// Add Gedmo extensions
$driverChain = new MappingDriverChain();
\Gedmo\DoctrineExtensions::registerAbstractMappingIntoDriverChainORM($driverChain, $cachedAnnotationReader);
// Set up driver to read annotations from entities
$annotationDriver = new AnnotationDriver($cachedAnnotationReader, 'src'));
$driverChain->addDriver($annotationDriver, 'App\Entity');
// General doctrine configuration
$doctrineConfig = new Configuration;
$doctrineConfig->setProxyDir(sys_get_temp_dir()));
$doctrineConfig->setProxyNamespace('App\Entity\Proxy');
$doctrineConfig->setAutoGenerateProxyClasses(false);
$doctrineConfig->setMetadataDriverImpl($driverChain);
$doctrineConfig->setMetadataCacheImpl($cache);
$doctrineConfig->setQueryCacheImpl($cache);
// Event manager to hook extensions
$evm = new EventManager();
// Tree extension
$treeListener = new \Gedmo\Tree\TreeListener;
$treeListener->setAnnotationReader($cachedAnnotationReader);
$evm->addEventSubscriber($treeListener);
// Create EntityManager
// $config['conn'] is connection credentials
return EntityManager::create($config['conn'], $doctrineConfig, $evm);
}
}
我的实体是:
<?php
namespace App\Entity;
use \Doctrine\ORM\Mapping as ORM;
use \Gedmo\Mapping\Annotation as Gedmo;
/**
* Class ProductCategory2
* @package App\Entity
*
* @Gedmo\Tree(type="nested")
* @ORM\Entity(repositoryClass="Gedmo\Tree\Entity\Repository\NestedTreeRepository")
*/
class ProductCategory2
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
*/
private $id;
/**
* @ORM\Column(type="string", length=50)
*/
private $name;
/**
* @Gedmo\TreeLeft
* @ORM\Column(type="integer")
*/
private $lft;
/**
* @Gedmo\TreeLevel
* @ORM\Column(type="integer")
*/
private $lvl;
/**
* @Gedmo\TreeRight
* @ORM\Column(type="integer")
*/
private $rgt;
/**
* @Gedmo\TreeRoot
* @ORM\Column(type="integer", nullable=true)
* @var
*/
private $root;
/**
* @Gedmo\TreeParent
* @ORM\ManyToOne(targetEntity="ProductCategory2", inversedBy="children")
*/
private $parent;
}
我的 cli-config.php 配置正确。 我 运行 doctrine cli 工具通过命令生成实体样板代码:
“vendor/bin/doctrine” orm:generate-entities src
它回答我:
Processing entity “Gedmo\Translatable\Entity\MappedSuperclass\AbstractPersonal\Translation”
Processing entity “Gedmo\Translatable\Entity\MappedSuperclass\AbstractTranslation”
Processing entity “Gedmo\Loggable\Entity\MappedSuperclass\AbstractLogEntry”
Processing entity “Gedmo\Tree\Entity\MappedSuperclass\AbstractClosure”
Processing entity “App\Entity\ProductCategory2”
实体工作正常,但命令将额外的文件添加到我的 src 文件夹中:
src\Gedmo
├───Loggable
│ └───Entity
│ └───MappedSuperclass/AbstractLogEntry.php
├───Translatable
│ └───Entity
│ └───MappedSuperclass/AbstractTranslation.php
└───Tree
└───Entity
└───MappedSuperclass/AbstractClosure.php
如果我通过上述命令再次生成实体,则会出现错误。
PHP Fatal error: Cannot redeclare class Gedmo\Loggable\Entity\MappedSuperclass\AbstractLogEntry in \src\Gedmo\Loggable\Entity\MappedSuperclass\AbstractLogEntry.php on line 9
要修复它,我需要先删除 <ROOT>/src/Gedmo
目录。
任何人都可以帮助找到配置中的错误以防止出现这种烦人的额外文件吗?
感谢帮助
我在 doctrine generate-entities 命令后添加了 hack 来清除烦人的目录。 cli-config.php 的完整列表如下:
<?php
use Doctrine\ORM\Tools\Console\ConsoleRunner;
use Interop\Container\ContainerInterface;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
use Symfony\Component\EventDispatcher\EventDispatcher;
require "vendor/autoload.php";
/** @var ContainerInterface $container */
$container = require 'app/bootstrap.php';
$dispatcher = new EventDispatcher();
// Post terminate cli command listener
$dispatcher->addListener(ConsoleEvents::TERMINATE, function(ConsoleTerminateEvent $event) {
$commandName = $event->getCommand()->getName();
switch($commandName) {
case 'orm:generate-entities':
// clear /src/Gedmo dir
\App\Utils\FilesystemUtils::removeDir('src/Gedmo');
break;
}
});
// Create doctrine cli environment via helper
$helperSet = ConsoleRunner::createHelperSet($container->get(\Doctrine\ORM\EntityManager::class));
// Wrap it into Symfony Console App and add some extra commands
$app = new Application('Doctrine Command Line Interface', \Doctrine\ORM\Version::VERSION);
$app->setDispatcher($dispatcher);
$app->setCatchExceptions(true);
$app->setHelperSet($helperSet);
// add default commands
ConsoleRunner::addCommands($app);
// here you may add extra commadts via $app->add(..)
$app->run();
官方文档: