Activerecord yii2:can';t从两个具有活动记录的表中获取数据
我想从两个表Activerecord yii2:can';t从两个具有活动记录的表中获取数据,activerecord,yii2,Activerecord,Yii2,我想从两个表注释,用户中获取数据 评论与用户有hasOne关系 public function getUser() { return $this->hasOne(Users::className(), ['user_id' => 'user_id']); } 我想从注释表中获取注释。注释id,注释。注释内容,注释。用户id,并使用。用户名,用户。用户显示用户表中的用户名以在gridview小部件中使用 我用 $res = Comments::find
注释,用户中获取数据
评论
与用户有hasOne
关系
public function getUser()
{
return $this->hasOne(Users::className(), ['user_id' => 'user_id']);
}
我想从注释
表中获取注释。注释id
,注释。注释内容
,注释。用户id
,并使用。用户名
,用户。用户显示用户
表中的用户名
以在gridview
小部件中使用
我用
$res = Comments::find()
->select([
'comments.comment_id',
'comments.comment_content',
'comments.user_id',
'users.user_id',
'users.user_display_name',
'users.user_name',
])
->innerJoinWith('user')
->all();
此代码获取注释
字段,但我无法从数据库中获取用户。用户名
,用户。用户名
我该怎么做
注意:数据库中的用户表是users
,但当我使用Gii创建模型时,关系方法声明为getUser()
,我不知道为什么
更新1:
注释
表格:
DROP TABLE IF EXISTS `comments`;
CREATE TABLE `comments` (
`comment_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`comment_content` text NOT NULL,
`comment_approved` enum('no','yes') NOT NULL DEFAULT 'no',
`comment_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`comment_parent` bigint(20) unsigned NOT NULL DEFAULT '0',
`sms_id` bigint(20) unsigned NOT NULL DEFAULT '0',
`user_id` bigint(20) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`comment_id`),
KEY `fk_comment_sms_id` (`sms_id`),
KEY `fk_commetn_user_id` (`user_id`),
CONSTRAINT `fk_comment_sms_id` FOREIGN KEY (`sms_id`) REFERENCES `sms` (`sms_id`),
CONSTRAINT `fk_commetn_user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8;
CREATE TABLE `users` (
`user_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`user_name` varchar(60) NOT NULL DEFAULT '',
`user_pass` varchar(64) NOT NULL DEFAULT '',
`user_level` int(11) NOT NULL DEFAULT '1',
`user_email` varchar(100) NOT NULL DEFAULT '',
`user_display_name` varchar(250) NOT NULL DEFAULT '',
`user_phone_number` varchar(11) NOT NULL,
`user_registered` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`user_activation_key` varchar(60) NOT NULL,
`user_status` enum('active','deactive','delete') NOT NULL DEFAULT 'deactive',
PRIMARY KEY (`user_id`),
UNIQUE KEY `u_user_sign` (`user_name`) USING BTREE,
UNIQUE KEY `u_user_email` (`user_email`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=24 DEFAULT CHARSET=utf8;
用户
表格:
DROP TABLE IF EXISTS `comments`;
CREATE TABLE `comments` (
`comment_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`comment_content` text NOT NULL,
`comment_approved` enum('no','yes') NOT NULL DEFAULT 'no',
`comment_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`comment_parent` bigint(20) unsigned NOT NULL DEFAULT '0',
`sms_id` bigint(20) unsigned NOT NULL DEFAULT '0',
`user_id` bigint(20) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`comment_id`),
KEY `fk_comment_sms_id` (`sms_id`),
KEY `fk_commetn_user_id` (`user_id`),
CONSTRAINT `fk_comment_sms_id` FOREIGN KEY (`sms_id`) REFERENCES `sms` (`sms_id`),
CONSTRAINT `fk_commetn_user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8;
CREATE TABLE `users` (
`user_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`user_name` varchar(60) NOT NULL DEFAULT '',
`user_pass` varchar(64) NOT NULL DEFAULT '',
`user_level` int(11) NOT NULL DEFAULT '1',
`user_email` varchar(100) NOT NULL DEFAULT '',
`user_display_name` varchar(250) NOT NULL DEFAULT '',
`user_phone_number` varchar(11) NOT NULL,
`user_registered` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`user_activation_key` varchar(60) NOT NULL,
`user_status` enum('active','deactive','delete') NOT NULL DEFAULT 'deactive',
PRIMARY KEY (`user_id`),
UNIQUE KEY `u_user_sign` (`user_name`) USING BTREE,
UNIQUE KEY `u_user_email` (`user_email`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=24 DEFAULT CHARSET=utf8;
我建议您将表重命名为comment
和user
。阅读这里:为什么你应该使用单数名称
Gii生成getUser
而不生成getUsers
的原因是hasOne
关系。它得到的是一个用户,而不是多个用户s-
您确定关系定义正确吗?user\u id
是用户表中的主键还是评论表中的FK?您确定数据库中有正确的数据吗?不确定如何显示或测试查询是否返回任何数据,但您应该阅读Yii中的关系
通过延迟加载,在您尝试访问数据之前,数据不存在,因此可能不会出现在诸如print\u r
或var\u dump
更新表查询中,我使用navicat在db中输入数据,我确信DBA中有正确的数据,如果您在foreach中执行print\r($comment)
,你看到用户名和显示用户名了吗?我在上面使用了@Alex help,这很有用。使用$comment->user->user\u name
。如何使用$res
显示数据?$res是模型对象(我想),我使用foreach($res as$comment)
,然后使用字段名获取值,如:$comment->comment\u content
。widget会自动使用它。您是否可以通过$comment->user->user\u name
yes:)、ty@Alex获得任何信息。我使用xdebug和var\u dump
查看结果,并且var\u dump
具有限制显示结果(我修复了它)。