我试图创建一对多的连接,但它的工作原理很奇怪。
我怀疑用户类有一个国家,而国家类有很多用户。但是 User->Country 会返回一个包含一个 Country 的数组(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); }
}
类 Country 扩展 sfDoctrineRecord { public function setTableDefinition() { $this->setTableName('country');
$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, ));
} }