我試圖創建一對多的連接,但它的工作原理很奇怪。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, ));
} }
嗯,我和YAML有同樣的問題。 但是,現在我知道thx prodigitalson,refClass強制多對多。 我只是在項目中有很多關係。 他們變化非常快(增加新的,使新的結構)。 這就是爲什麼它是一個好主意,使純模式,表和關係,表獨立的表。 對象也有很多鑰匙對我來說並不好。 而這樣的黑客在型號只有一條路? (( – burgua
@burgua:是的,YAML不會讓這裏的差別這是Dcotrine工作,無論你的模型是如何構建的方式我不是爲什麼發售者,你需要有針對的arent M2M關係一個單獨的表,無論有多快。您的架構更改。你必須糾正類時發生這種情況。如果你使用YAML那麼簡單,作爲執行一些任務,也許定製生成的遷移,如果你需要保持DB數據完好無損,而另一方面如果您選擇手動做的事情那是一個頭疼的 - 但使用'refClass'不是:-) – prodigitalson
@burgua的解決方案:或者,你說的是實現與學說 – prodigitalson