2011-01-16 24 views
1

我試圖創建一對多的連接,但它的工作原理很奇怪。Doctrine中的一對多連接

我懷疑這個類的用戶有一個國家,而國家有很多用戶。 但用戶 - >國家返回一個數組與一個國家,(學說收集,而不是一個記錄)。

有沒有人知道爲什麼?

  • 我需要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() { $這 - > 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對象,並且我知道, 這樣的關係可以在沒有 附加對象的情況下進行。

但這是你的問題的答案。你可以使用refClass來設置它作爲多對多,因爲你總是在訪問關係時去取得一個Doctrine_Collection。這是它的工作原理,我不知道爲什麼你會期望一個單一的對象。

如果集合中只有一個記錄,那麼您可能會覆蓋訪問器以僅返回一條記錄,但這很可能會破壞事情,因爲所有事情都將期望從此返回Doctine_Collection訪問:

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

真正的解決辦法是使用正確類型的關係......

而且......如果youre爲什麼在神的名字使用的Symfony,你不使用YAML定義是什麼?它更容易使用和管理。

+0

嗯,我和YAML有同樣的問題。 但是,現在我知道thx prodigitalson,refClass強制多對多。 我只是在項目中有很多關係。 他們變化非常快(增加新的,使新的結構)。 這就是爲什麼它是一個好主意,使純模式,表和關係,表獨立的表。 對象也有很多鑰匙對我來說並不好。 而這樣的黑客在型號只有一條路? (( – burgua

+0

@burgua:是的,YAML不會讓這裏的差別這是Dcotrine工作,無論你的模型是如何構建的方式我不是爲什麼發售者,你需要有針對的arent M2M關係一個單獨的表,無論有多快。您的架構更改。你必須糾正類時發生這種情況。如果你使用YAML那麼簡單,作爲執行一些任務,也許定製生成的遷移,如果你需要保持DB數據完好無損,而另一方面如果您選擇手動做的事情那是一個頭疼的 - 但使用'refClass'不是:-) – prodigitalson

+0

@burgua的解決方案:或者,你說的是實現與學說 – prodigitalson