2011-10-27 5 views
1

私はいくつかのフォームを持ち、1つのカラムにはEntityタイプがありますが、このエンティティには別の接続があります。アクションでフォームクラスとDoctrine Fixturesでのdoctrine接続の変更

私は

$em->getDoctrine()->getEntityManager('name')は、どのようにフォームクラスでの接続を変更することができますか?

エンティティクラスで接続を変更する可能性があります。

が、私はここに答え:)

http://symfony.com/doc/2.0/reference/forms/types/entity.html#em

を見つけました。しかし、私はデータフィクスチャクラスの接続を変更する方法:この

orm: 
    default_entity_manager: default 
    entity_managers: 
     owner: 
      connection: owner 
      mappings: 
       RealestateCoreBundle: 
        Entity: MyEntity 

同様 が更新しますか?

は、私が試してみてください。

<?php 

namespace Realestate\CoreBundle\DataFixtures\ORM; 

use Doctrine\Common\DataFixtures\FixtureInterface; 
use Realestate\CoreBundle\Entity\Owner; 
use Symfony\Component\DependencyInjection\ContainerAwareInterface; 
use Symfony\Component\DependencyInjection\ContainerInterface; 

class OwnerFixtures implements FixtureInterface, ContainerAwareInterface 
{ 

    private $container; 

    public function setContainer(ContainerInterface $container = null) 
    { 
     $this->container = $container; 
    } 

    public function load($manager) 
    { 
     $this->container->get('doctrine')->getEntityManager('owner'); 

     for ($i = 0; $i < 100; $i++) { 
      $owner = new Owner(); 
      $owner->setName('name-' . $i); 
      $owner->setTelephone(mt_rand(100000, 999999)); 
      $manager->persist($owner); 
     } 

     $manager->flush(); 
    } 

} 

しかしdidntの仕事:(

答えて

0

備品エンティティマネージャ変更するには、コンソールコマンドを実行するときにフラグを使用することができますロード:

Executing Fixtures

php app/console doctrine:fixtures:load --em=manager_name 

また、このセクションをチェックすることもできますeは、同じドキュメント:

Using the container in fixtures

あなたのフィクスチャクラスは、コンテナへのアクセス権を持っている場合、あなたはあなたが望む任意のエンティティマネージャを読み込むことができます。

$container->get('doctrine')->getEntityManager('manager_name'); 
+0

$ container-> get( 'doctrine') - > getEntityManager( 'manager_name'); ' doest work – rtyshyk

+0

True ...あなたの什器がコンテナを認識しているかぎり、$ this-> container-> get( 'doctrine') - > getEntityManager( 'manager_name');上記のリンク。これをこのようにしたい場合は重要なビットですが、あなたのフィクスチャは 'ContainerAwareInterface'を実装しなければなりません。しかし、コマンドラインでマネージャーの名前を渡す方が簡単な方法かもしれません。 – Kasheen

+0

更新の質問。 – rtyshyk

0

あなたのフィクスチャはconfig.ymlであなたの実際の設定とコンテナへのアクセス権を持っている場合: orm: default_entity_manager: defaultは、ここにコード entity_managers: owner: connection: owner mappings: RealestateCoreBundle: Entity: MyEntity

を入力してくださいあなたがそのようなエンティティマネージャを呼び出すことができます。

$manager = $this->container->get('doctrine.orm.owner_entity_manager');

関連する問題