我正在使用cross-join
Doctrine_RawSql 加载 3 个不同的表。这让我回到了以下对象:
User -> User class (doctrine base class)
Settings -> DoctrineCollection of Setting
User_Settings -> DoctrineCollection of User_Setting
上面的对象是一个many-to-many
关系的结果User
,Setting
whereUser_Setting
作为一个引用表。User_Setting
还包含另一个名为value
. 这显然包含了对应的值Setting
。
到目前为止一切都很好,但是返回对象的Settings
和User_Settings
属性绝不会相互关联(当然除了字段)。User
setting_id
有没有直接从Settings
属性遍历到对应User_Settings
属性的直接方法?
这是相应的查询:
$sets = new Doctrine_RawSql();
$sets->select('{us.*}, {s.*}, {uset.*}')
->from('(User us CROSS JOIN Setting s) LEFT JOIN User_Setting uset ON us.user_id = uset.user_id AND s.setting_id = uset.setting_id')
->addComponent('us', 'User us')
->addComponent('uset', 'us.User_Setting uset')
->addComponent('s', 'us.Setting s')
->where('s.category_id = ? AND us.user_id = ?',array(1, 1));
$sets = $sets->execute();
编辑:
1:这是相关的 YAML 标记
//User relations:
Setting:
class: Setting
foreignAlias: User
refClass: User_Setting
local: user_id
foreign: setting_id
//Setting relations:
User:
class: User
foreignAlias: Setting
refClass: User_Setting
local: setting_id
foreign: user_id
//User_Setting relations:
Setting:
foreignAlias: User_Setting
local: setting_id
foreign: setting_id
User:
foreignAlias: User_Setting
local: user_id
foreign: user_id
2. 这是目标代码(从 YAML 生成):
//BaseUser setup()
$this->hasMany('Setting', array(
'refClass' => 'User_Setting',
'local' => 'user_id',
'foreign' => 'setting_id'));
$this->hasMany('User_Setting', array(
'local' => 'user_id',
'foreign' => 'user_id'));
//BaseSetting setup()
$this->hasMany('User', array(
'refClass' => 'User_Setting',
'local' => 'setting_id',
'foreign' => 'user_id'));
$this->hasMany('User_Setting', array(
'local' => 'setting_id',
'foreign' => 'setting_id'));
//BaseUser_Setting setup()
$this->hasOne('Setting', array(
'local' => 'setting_id',
'foreign' => 'setting_id'));
$this->hasOne('User', array(
'local' => 'user_id',
'foreign' => 'user_id'));