问题
For a better user experience, I didn't want to just make a casual PM system, but actually group it into conversations. And as an added feature, being able to PM multiple people at once. Something that was requested multiple times. So I created this:
/*
User conversations
ONE Conversation
HAS MANY Conversation Members
HAS MANY Users
HAS MANY Private Messages
ONE Private Message
HAS ONE Conversation
ONE User
HAS MANY Conversation Member
HAS ONE Conversation
*/
CREATE TABLE IF NOT EXISTS `tbl_user_pm_conv_members` (
`user_id` int(11),
`conv_id` int(11)
);
CREATE TABLE IF NOT EXISTS `tbl_user_pm_conv` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`owner_id` int(11) NOT NULL,
`subject` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE IF NOT EXISTS `tbl_user_pm_msg` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`conv_id` int(11) NOT NULL,
`from_id` int(11) NOT NULL,
`body` text NOT NULL,
`sent` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
);
I hope this actually makes sense...
Now, I wanted to setup the n:m relation in Yii (1.1.16 currently). But no matter what I do, it does not want to resolve.
In User.php:
public function relations() {
return array(
'profile'=>array(self::HAS_ONE, 'UserProfile', 'uID'),
'updates'=>array(self::HAS_MANY, "UserUpdate", "tID"),
'permissions'=>array(self::HAS_MANY, "UserPermissions", "uID"),
'settings'=>array(self::HAS_ONE, "UserSettings", "id"),
'convos'=>array(
self::MANY_MANY, "PrivateConversation",
"tbl_user_pm_conv_members(id,id)",
)
);
}
By Yii's logic, my FKs should probably have been user_id
and convo_id
, resulting in tbl_user_pm_conv_members(user_id, convo_id)
. I also do not have MySQL constraints set, as I haven't understood them yet.
When I try to print_r($user->convos)
, I get this:
2015/04/28 23:49:01 [error] [system.db.CDbCommand] CDbCommand::fetchAll() failed: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'convos_convos.id' in 'on clause'. The SQL statement executed was: SELECT `convos`.`id` AS `t1_c0`, `convos`.`owner_id` AS `t1_c1`, `convos`.`subject` AS `t1_c2` FROM `tbl_user_pm_conv` `convos` INNER JOIN `tbl_user_pm_conv_members` `convos_convos` ON (`convos_convos`.`id`=:ypl0) AND (`convos`.`id`=`convos_convos`.`id`).
2015/04/28 23:49:01 [error] [exception.CDbException] exception 'CDbException' with message 'CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'convos_convos.id' in 'on clause'' in /Users/Ingwie/Work/BIRD3/php_modules/yiisoft/yii/framework/db/CDbCommand.php:543
I am lost. What is the right way to set up the n:m relation?
回答1:
You have an error in:
"tbl_user_pm_conv_members(id,id)",
It should be:
"tbl_user_pm_conv_members(user_id,conv_id)",
来源:https://stackoverflow.com/questions/29931920/yii-relate-nm-on-both-sides-of-an-ar