这是我的数据库结构:
CREATE TABLE `rs_payment` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`amount` int(11) NOT NULL DEFAULT '0',
`user_fid` int(11) NOT NULL,
`payment_created_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`payment_end_date` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `user_fid` (`user_fid`),
CONSTRAINT `rs_payment_ibfk_1` FOREIGN KEY (`user_fid`) REFERENCES `rs_user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `rs_payment_to_complex` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`payment_fid` int(11) NOT NULL,
`complex_fid` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `payment_fid` (`payment_fid`),
KEY `complex_fid` (`complex_fid`),
CONSTRAINT `rs_payment_to_complex_ibfk_2` FOREIGN KEY (`complex_fid`) REFERENCES `rs_complex` (`id`),
CONSTRAINT `rs_payment_to_complex_ibfk_1` FOREIGN KEY (`payment_fid`) REFERENCES `rs_payment` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `rs_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(258) NOT NULL,
`password` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `rs_complex` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` text NOT NULL,
`reservation_schedule` tinyint(4) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
这些是我在PaymentToComplex模型中的关系:
public function relations()
{
return array(
'complexF' => array(self::BELONGS_TO, 'Complex', 'complex_fid'),
'paymentF' => array(self::BELONGS_TO, 'Payment', 'payment_fid'),
'userF' => array(self::BELONGS_TO, 'User', 'payment_fid'),
//'userF' => array(self::BELONGS_TO, 'User', 'paymentF.user_fid'),
);
}
复杂和支付关系运作良好。用户关系实际上已加入查询,如下所示:LEFT OUTER JOIN rs_user userF ON (t.payment_fid=userF.id)
。
我需要更新userF
的外键以加入user_fid
表的rs_payment
列。注释代码不起作用。
感谢您的帮助。
答案 0 :(得分:1)
您应该使用CActiveRecord::through
使用userF
获取paymentF
,如下所示:
'userF' => array(self::BELONGS_TO, 'User', array('user_fid'=>'id'),'through'=>'paymentF'),
更多http://www.yiiframework.com/doc/guide/1.1/en/database.arr#relational-query-with-through