2011-01-16 16 views
1

私は1対多の接続を作成しようとしましたが、それは非常に奇妙です。Doctrineの1対多の接続

私はクラスUserに1つのCountryがあり、Countryには多くのユーザーがいると思われます。 しかし、User-> Countryは1つの国(Doctrine Collection、Recordではない)を持つ配列を返します。

理由は誰ですか?

  • 私はCountryUserオブジェクトが必要、と私はそのような関係は、追加のオブジェクトなし を行うことができることを、知っている 。
  • 私は ではありません。Doctrine用のYAML形式を使用しています。 クラスは手動で作成されています。

    クラスユーザーはsfDoctrineRecord {

    public function setTableDefinition() 
    { 
        $this->setTableName('user'); 
        $this->hasColumn('id', 'integer', 5, array(
         'type' => 'integer', 
         'primary' => true, 
         'unsigned' => true, 
         'autoincrement' => true, 
         'length' => 5, 
         )); 
        $this->hasColumn('fbid', 'string', 40, array(
         'type' => 'string', 
         'length' => 40, 
         #'notnull' => true, 
         #'unique' => true, 
         )); 
    } 
    
    public function setUp() 
    { 
        parent::setUp(); 
        $this->hasOne('Country', array(
         'local' => 'user_id', 
         'foreign' => 'country_id', 
         'refClass' => 'CountryUser' 
        )); 
    
        $timestampable0 = new Doctrine_Template_Timestampable(array(
         )); 
        $this->actAs($timestampable0); 
    } 
    

    }

    クラスの国がsfDoctrineRecord 拡張{ パブリック関数のsetTableDefinition()を拡張し { ます$ this-> setTableName( '国');

    $this->hasColumn('id', 'integer', 5, array(
         'type' => 'integer', 
         'primary' => true, 
         'unsigned' => true, 
         'autoincrement' => true, 
         'length' => 5, 
         )); 
    
        $this->hasColumn('name', 'string', 10, array(
         'type' => 'string', 
         'length' => 10, 
         'unique' => true, 
         #'notnull' => true, 
        )); 
    } 
    
    public function setUp() 
    { 
        parent::setUp(); 
    
        $this->hasMany('User as Users', array(
         'local' => 'country_id', 
         'foreign' => 'user_id', 
         'refClass' => 'CountryUser' 
        )); 
    
        $timestampable0 = new Doctrine_Template_Timestampable(array(
         )); 
        $this->actAs($timestampable0); 
    } 
    

    }

    クラスCountryUserがsfDoctrineRecord {

    パブリック関数setTableDefinition(){

    $this->setTableName('country_user'); 
    
    $this->hasColumn('user_id', 'integer', 5, array(
        'notnull' => true, 
        'unsigned' => true, 
        'length' => 5, 
        'type' => 'integer', 
        'primary' => true, 
    )); 
    
    $this->hasColumn('country_id', 'integer', 5, array(
        'type' => 'integer', 
        'notnull' => true, 
        'unsigned' => true, 
        'length' => 5, 
        'primary' => true, 
    )); 
    

    }}

答えて

1
を拡張

CountryUserオブジェクトが必要ですが、 追加オブジェクトなしでこのような関係が成立することがわかっています。

しかし、それはあなたの質問に対する答えです。関係をアクセスするときにDoctrine_Collectionを取得するためには、常にrefClassを使用して多対多として設定してください。これはどのように動作するのですか、単一のオブジェクトが期待される理由はわかりません。

コレクションに1つのレコードしかない場合は、単一のレコードを返すようにアクセサーをオーバーライドすることはできますが、これはDoctine_Collectionがこれから返されることをすべて期待しているためアクセサは:youreのあなたがYAMLの定義を使用していない理由は神の名でのSymfonyを使用している場合

class User extends sfDoctrineRecord { 
    public function getCountry(
     $coll = $this->_get('Country'); 
     if($coll->count() == 1){ 
      return $coll->getFirst(); 
     } 
     else 
     { 
      return $coll; 
     } 
    } 
} 

真の解決策は... ...関係の適切なタイプを使用することも

のですか?その使用と管理が非常に簡単です。

+0

私はYAMLと同じ問題がありました。 しかし、今や私はrefClassが多対多を強制することを知っています。 私はちょうどプロジェクトで本当にたくさんの関係を持っています。 そして、それらは非常に速く変化します(新しい構造を追加し、新しい構造を作ります)。 純粋なモデル - テーブルとリレーションテーブルのために別々のテーブルを作るのは良い考えでした。 オブジェクトにもたくさんのキーを置くことは、私にとって素晴らしいことではありませんでした。 モデルのそのようなハックは片方だけですか? (( – burgua

+0

@ブルグア:はい、YAMLはここで違いを見ません)これは、あなたのモデルがどのように構築されているかにかかわらず、Dcotrineが動作する方法です。あなたのスキーマが変更されます。そのときにクラスを修正する必要があります... yamlを使用する場合、いくつかのタスクを実行するだけで簡単ですし、dbデータをそのまま維持する必要がある場合は、もしあなたが手作業で頭痛を負っているのであれば、 'refClass'を使うことは解決策ではありません:-) – prodigitalson

+0

@burgua:DoctrineでEAVモデルを実装することについて話していますか? – prodigitalson

関連する問題