スタンドアロンの noframework アプリケーションでGedmo 拡張機能を備えた Doctrine を使用しています。自動ロードは、composer の composer.json コンテンツを介して行われます。
{
"autoload": {
"psr-0": {
"App": "src"
}
},
"require": {
"doctrine/orm": "^2.5",
"gedmo/doctrine-extensions": "^2.4"
}
}
アプリのコア クラスは /src ディレクトリに配置され、composer ファイルは /vendor に配置されます。Doctrine はファクトリ経由で構成され、そのメイン コードは以下のとおりです。
<?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
それは私に答えます:
処理エンティティ「Gedmo\Translatable\Entity\MappedSuperclass\AbstractPersonal\Translation」</p>
処理エンティティ「Gedmo\Translatable\Entity\MappedSuperclass\AbstractTranslation」</p>
処理エンティティ「Gedmo\Loggable\Entity\MappedSuperclass\AbstractLogEntry」</p>
処理エンティティ「Gedmo\Tree\Entity\MappedSuperclass\AbstractClosure」</p>
処理エンティティ「App\Entity\ProductCategory2」</p>
エンティティは正常に動作していますが、コマンドによって余分なファイルが src フォルダーに追加されます。
src\Gedmo
├───Loggable
│ └───Entity
│ └───MappedSuperclass/AbstractLogEntry.php
├───Translatable
│ └───Entity
│ └───MappedSuperclass/AbstractTranslation.php
└───Tree
└───Entity
└───MappedSuperclass/AbstractClosure.php
前述のコマンドを使用してエンティティをもう一度生成すると、エラーが発生します。
PHP 致命的なエラー: 9 行目の \src\Gedmo\Loggable\Entity\MappedSuperclass\AbstractLogEntry.php のクラス Gedmo\Loggable\Entity\MappedSuperclass\AbstractLogEntry を再宣言できません
それを修正するには、<ROOT>/src/Gedmo
前にディレクトリを削除する必要があります。
この迷惑な余分なファイルが表示されるのを防ぐために、config のバグを見つけるのを手伝ってくれる人はいますか?
助けてくれてありがとう