2017-06-26 4 views
0

私は、拡張性をさらに高めるため抽象モデルを持つSymfony 3バンドルを作成しました。 ユーザーのようなものがFOS \ UserBundleのモデルです。しかし、FOSのBundleとは異なり、私のニーズはこれらのModels \ Entities間の関係も持つ必要があります。私はInterfacesとDoctrineの少しの(hehe、ほぼバイト)設定でそれをやっていました。ですから、今のところ、私は設定するには2つのことをしなければなりません - 私の\ CustomBundleと言いましょう。カスタムバンドル設定のdoctrine resolve_target_entities

アプリ/設定/ config.ymlでは、私は追加する必要があります。

[...] 
doctrine: 
    orm: 
[...] 
     resolve_target_entities: 
      My\CustomBundle\Model\Entity1Interface: AppBundle\Entity\Entity1 
      My\CustomBundle\Model\Entity2Interface: AppBundle\Entity\Entity2 
[...] 

そしてまた私の\ CustomBundle構成:

my_custom: 
    doctrine: 
     entity_1_class: AppBundle\Entity\Entity1 
     entity_2_class: AppBundle\Entity\Entity2 

をあなたが見ることができるように、構成が少し厄介かつ反復的ですdoctrine.ormセクションのために。私はMy \ CustomBundleで自動的に行うことでそれを避けることができたらいいと思う。そうする可能性はありますか?

答えて

0

コンパイラパスの助けを借りて作ることができます。コンパイルパスを追加して、をmy_customバンドル設定に従って変更します。

CompilerPass:

namespace My\CustomBundle\DependencyInjection\Compiler; 

use Doctrine\ORM\Version; 
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; 
use Symfony\Component\DependencyInjection\ContainerBuilder; 

class DoctrineResolveTargetEntityPass implements CompilerPassInterface 
{ 
    /** 
    * {@inheritdoc} 
    */ 
    public function process(ContainerBuilder $container) 
    { 
     // resolve_target_entities 
     $definition = $container->findDefinition('doctrine.orm.listeners.resolve_target_entity'); 

     $definition->addMethodCall('addResolveTargetEntity', array(
      $container->getParameter('my_custom.doctrine.entity_1.resolve_from'), // My\CustomBundle\Model\Entity1Interface 
      $container->getParameter('my_custom.doctrine.entity_1.resolve_to'), // AppBundle\Entity\Entity1 
      array(), 
     )); 

     if (version_compare(Version::VERSION, '2.5.0-DEV') < 0) { 
      $definition->addTag('doctrine.event_listener', array('event' => 'loadClassMetadata')); 
     } else { 
      $definition->addTag('doctrine.event_subscriber', array('connection' => 'default')); 
     } 
    } 
} 

そして、あなたのバンドルの魅力のような

namespace My\CustomBundle; 

use Symfony\Component\HttpKernel\Bundle\Bundle; 
use Symfony\Component\DependencyInjection\ContainerBuilder; 
use Symfony\Component\DependencyInjection\Compiler\PassConfig; 
use My\CustomBundle\DependencyInjection\Compiler\DoctrineResolveTargetEntityPass; 

class MyCustomBundle extends Bundle 
{ 
    public function build(ContainerBuilder $container) 
    { 
     parent::build($container); 
     $container->addCompilerPass(new DoctrineResolveTargetEntityPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 1000); 
    } 
} 
+0

Worksでこのコンパイラパスを登録します。そして、doctrine.dbal.typesでそれを行う可能性はありますか?私はバンドル内で2つのカスタム列挙型を使用しています。バンドルを有効にした後、自動的にそれらをアプリケーションに挿入したいと考えています。 – MountainDev

+0

doctrine.dbal.typesを使ってこのトリックをどのように達成することができますか? Doctrineで正確なイベントリスナーが見つかりません – MountainDev

+0

私はバンドルにカスタムタイプがありませんでした。 –

関連する問題