gpt4 book ai didi

php - Yii:在 AR 的两边关联 n:m

转载 作者:行者123 更新时间:2023-11-29 00:00:54 25 4
gpt4 key购买 nike

为了更好的用户体验,我不想只是做一个随便的PM系统,而是把它实际分组到对话中。作为一项附加功能,能够同时向多人发送消息。多次请求的东西。所以我创建了这个:

/*
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`)
);

我希望这真的有意义......

现在,我想在 Yii(当前为 1.1.16)中设置 n:m 关系。但是无论我做什么,它都不想解决。

在 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)",
)
);
}

根据 Yii 的逻辑,我的 FK 可能应该是 user_idconvo_id,结果是 tbl_user_pm_conv_members(user_id, convo_id)。我也没有设置 MySQL 约束,因为我还不了解它们。

当我尝试 print_r($user->convos) 时,我得到了这个:

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

我迷路了。建立 n:m 关系的正确方法是什么?

最佳答案

你有一个错误:

"tbl_user_pm_conv_members(id,id)",

应该是:

"tbl_user_pm_conv_members(user_id,conv_id)",

关于php - Yii:在 AR 的两边关联 n:m,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29931920/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com