I have tried to create one-to-many connection, but it works very strange.
I suspect that class User has one Country, and class Country has many Users. But User->Country return ever an array with one Country, (Doctrine Collection, not a Record).
Have anyone idea why?
- I need CountryUser object, and I know, that such relation can be made without additional object.
I'm not using YAML format for Doctrine, classes are made manualy.
class User extends 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); }
}
class Country extends 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); }
}
class CountryUser extends sfDoctrineRecord {
public function setTableDefinition() {
$this->setTableName('country_user'); $this->hasColumn('user_id', 'integer', 5, array( 'notnull' => true, 'unsigned' => true, 'length' => 5, 'type' => 'integer', 开发者_如何学Go'primary' => true, )); $this->hasColumn('country_id', 'integer', 5, array( 'type' => 'integer', 'notnull' => true, 'unsigned' => true, 'length' => 5, 'primary' => true, ));
} }
I need CountryUser object, and I know, that such relation can be made without additional object.
But that is the answer to your question. Youre setting it up as a many-to-many by using the refClass
as such youre always goign to get a Doctrine_Collection when accessing the relation. This is how it works, im not sure why you would expect a single object.
You might be able to override the accessor to only return a single record if there is only one in the collection, but this is most likely going to break things because everything is going to be expecting a Doctine_Collection to be returned from this accessor:
class User extends sfDoctrineRecord {
public function getCountry(
$coll = $this->_get('Country');
if($coll->count() == 1){
return $coll->getFirst();
}
else
{
return $coll;
}
}
}
The real solution is to use the proper type of relationship...
Also... if youre using Symfony why in gods name are you not using the YAML definitions? Its so much easier to use and manage.
精彩评论