2017-08-29 8 views
0

私はGenericエンティティリファレンスを扱うフォームタイプを構築しています。これは、あるエンティティを他のエンティティと関連付けることを可能にします。私はすべてのphpを検索するFinderコンポーネントを使用してきたこれまでのところDoctrineからすべてのマップされたエンティティクラスを取得するには?

class GenericType extends AbstractType 
{ 
    // ... 

    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder->add('entityClass', ChoiceType::class, [ 
      'choices' => $this->getAllMappedEntityClasses(), 
     ]); 

     // ... 
    } 

    private function getAllMappedEntityClasses() 
    {   
     // populating $choices with all current entity classes 

     return $choices; 
    } 
} 

class Generic 
{ 
    // ... 

    /** 
    * @ORM\Column(type="string") 
    */ 
    $private $entityClass; 
} 

マイフォームタイプがこれです:

このGenericは、参照先エンティティクラスを保存$entityClass性質を持っていますそれぞれのEntityディレクトリ内のクラスを手で、$class = $file->getBasename('.php');require_once $file;まで私はすべてのクラスを与えます。しかし、私はコードサンプルでそれを省略している理由です、それは恐ろしい回避策だと認識しています:(

現在、私のアプリケーションにマッピングされたエンティティクラスのリストを取得するための別の方法はありますか?思考?

答えて

1

TL ; DRは、フォームタイプにEntityManagerサービスを注入し、これを試してみてください。

/** @var \Doctrine\ORM\Mapping\ClassMetadata[] $metadata */ 
$metadata = $this->em->getMetadataFactory()->getAllMetadata(); 

$choices = []; 
foreach($metadata as $classMeta) { 
    $choices[] = $classMeta->getName(); // Entity FQCN 
} 

return $choices; 

これは、すべての登録済みのエンティティからの完全修飾クラス名を返します

関連する問題