我在獨立noframework應用程序中使用原則。 自動加載經由作曲家完成,composer.json內容:Doctrine生成實體命令會生成額外的src/Gedmo文件,這將導致命令停止工作,直到刪除文件夾爲止
{
"autoload": {
"psr-0": {
"App": "src"
}
},
"require": {
"doctrine/orm": "^2.5",
"gedmo/doctrine-extensions": "^2.4"
}
}
應用核心類置於/ src目錄,作曲家文件放置在商/賣方 學說經由工廠配置的,它的主要代碼如下:
<?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文件的配置是否正確。 我跑學說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
之前。
有人可以幫助找到配置中的錯誤,以防止這些惱人的額外文件出現?
感謝幫助
清除緩存爲我做到了: - | –