Doctrine 中的一对多连接
我尝试创建一对多连接,但效果很奇怪。
我怀疑 User 类有一个 Country,而 Country 类有许多 Users。 但用户->国家/地区返回一个包含一个国家/地区的数组(学说集合,而不是记录)。
有谁知道为什么吗?
- 我需要 CountryUser 对象,并且我 知道,这样的关系可以建立 没有额外的对象。
我不是 使用 YAML 格式进行 Doctrine, 类是手动创建的。
类 User 扩展了 sfDoctrineRecord {
公共函数 setTableDefinition() { $this->setTableName('用户'); $this->hasColumn('id', '整数', 5, 数组( '类型' => '整数', '主要' =>真的, '无符号' =>真的, '自动增量' =>真的, '长度' => 5、 )); $this->hasColumn('fbid', '字符串', 40, 数组( '类型' => '细绳', '长度' => 40、 #'notnull'=>真的, #'独特'=>真的, )); } 公共函数setUp() { 父级::setUp(); $this->hasOne('国家', array( '本地' => '用户身份', '外国' => '国家ID', 'refClass'=>; '国家用户' )); $timestampable0 = new Doctrine_Template_Timestampable(数组( )); $this->actAs($timestampable0); }
}
类国家扩展了sfDoctrineRecord { 公共函数 setTableDefinition() { $this->setTableName('国家');
$this->hasColumn('id', '整数', 5, array( '类型' => '整数', '主要' =>真的, '无符号' =>真的, '自动增量' =>真的, '长度' => 5、 )); $this->hasColumn('名称', '字符串', 10, 数组( '类型' => '细绳', '长度' => 10、 '独特' =>真的, #'notnull'=>真的, )); } 公共函数setUp() { 父级::setUp(); $this->hasMany('用户为用户', array( '本地' => '国家ID', '外国' => '用户身份', 'refClass'=>; '国家用户' )); $timestampable0 = new Doctrine_Template_Timestampable(数组( )); $this->actAs($timestampable0); }
}
类 CountryUser 扩展 sfDoctrineRecord {
公共函数setTableDefinition() {
$this->setTableName('country_user'); $this->hasColumn('user_id', '整数', 5, array( 'notnull'=>;真的, '无符号' =>真的, '长度' => 5、 '类型' => '整数', '主要' =>真的, )); $this->hasColumn('country_id', 'integer', 5, array( '类型' => '整数', 'notnull'=>;真的, '无符号' =>真的, '长度' => 5、 '主要' =>真的, ));
} 不是
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', 'primary' => true, )); $this->hasColumn('country_id', 'integer', 5, array( 'type' => 'integer', 'notnull' => true, 'unsigned' => true, 'length' => 5, 'primary' => true, ));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
但这就是你问题的答案。您通过使用 refClass 将其设置为多对多,因此在访问关系时您总是会获得 Doctrine_Collection。这就是它的工作原理,我不确定为什么你会期望一个单一的对象。
都将期望从此访问器返回 Doctine_Collection:
如果集合中只有一条记录,您也许能够重写访问器以仅返回一条记录,但这很可能会破坏事情,因为所有内容 解决方案是使用正确的关系类型...
另外...如果你使用 Symfony 为什么你不使用 YAML 定义?它更容易使用和管理。
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:
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.