Yii:在AR的两侧关联n:m [英] Yii: relate n:m on both sides of an AR
问题描述
为了获得更好的用户体验,我不想只是创建一个休闲的PM系统,而是将其实际分组到对话中。作为一项附加功能,能够一次PM多人。多次请求的内容。所以我创建了这个:
/ *
用户对话
一次对话
有许多对话成员
有许多用户
有许多私人消息
一条私人消息
有一次对话
一名用户
有许多会话成员
有一个会话
* /
如果不存在则创建表`tbl_user_pm_conv_members`(
`user_id` int(11),
`conv_id `int(11)
);
如果不存在则创建表(tbl_user_pm_conv`(
`id` int(11)NOT NULL AUTO_INCREMENT,
`owner_id` int(11)NOT NULL,
`subject` varchar (255)NOT NULL,
主键(ʻid`)
);
如果不存在则创建表(tbl_user_pm_msg`(
`id` int(11)NOT NULL AUTO_INCREMENT,
`conv_id` int(11)NOT NULL,
`from_id` int (11)NOT NULL,
`body`文本NOT NULL,
`sent` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY(ʻid`)
);
我希望这确实有意义...
现在,我想在Yii中设置n:m关系(当前为1.1.16)。但是无论我做什么,它都不想解决。
在User.php中:
公共函数关系(){
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),
)
);
}
按照Yii的逻辑,我的FK应该应该是 user_id
和 convo_id
,导致 tbl_user_pm_conv_members(user_id,convo_id)
。我还没有设置MySQL约束,因为我还不了解它们。
当我尝试 print_r($ user-> convos)
,我得到这个:
2015/04/28 23:49:01 [错误] [system.db.CDbCommand] CDbCommand :: fetchAll()失败:SQLSTATE [42S22]:找不到列:1054 on子句中的未知列 convos_convos.id。执行的SQL语句为:选择`convos`.id`AS`t1_c0`,`convos`.`owner_id`AS`t1_c1`,`convos`.subject`AS`t1_c2` from`tbl_user_pm_conv`加入`tbl_user_pm_conv_members``convos_convos` ON(`convos_convos`.ʻid` =:ypl0)和(`convos`.ʻid` =`convos_convos`.id`)。
$ p中 on子句中的convos_convos.id' $ p>
2015/04/28 23:49:01 [错误] [exception.CDbException]异常'CDbException'和消息'CDbCommand无法执行SQL语句:SQLSTATE [42S22]:找不到列:1054未知列' /Users/Ingwie/Work/BIRD3/php_modules/yiisoft/yii/framework/db/CDbCommand.php:543
我迷路了。设置n:m关系的正确方法是什么?
解决方案您的输入错误:
tbl_user_pm_conv_members(id,id),
应为:
tbl_user_pm_conv_members(user_id,conv_id),
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
andconvo_id
, resulting intbl_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?
解决方案You have an error in:
"tbl_user_pm_conv_members(id,id)",
It should be:
"tbl_user_pm_conv_members(user_id,conv_id)",
这篇关于Yii:在AR的两侧关联n:m的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!