我正在尝试显示所有博客,无论是否有与之相关的评论但是我有1个问题,因为它们如下:
这是我的模特:
类A_User_Blog_Comments_model扩展CI_Model {
public function get_blog($id) {
$this->db->select('
entry.user_id,
entry.entry_id,
entry.entry_name,
entry.entry_body,
entry.status,
entry.created_timestamp,
entry.updated_timestamp,
comments.id,
comments.comment,
comments.user_id,
comments.blog_id,
comments.status,
comments.created_timestamp,
comments.updated_timestamp
');
$this->db->from('entry');
$this->db->join('comments', 'entry.entry_id=comments.blog_id', 'left');
$this->db->where('entry.entry_id',$id);
$query = $this->db->get();
if($query->num_rows() != 0)
{
return $query->result_array();
}
else
{
return false;
}
}
以下是我的视图文件中的简单转储:
Array
(
[0] => Array
(
[user_id] => 1
[entry_id] => 1
[entry_name] => twkla nnn xxx
[entry_body] => this is just UPDATED
[status] => active
[created_timestamp] => 2017-01-03 00:00:00
[updated_timestamp] => 2017-01-04 00:00:00
[id] => 1
[comment] => This is a comment
[blog_id] => 1
)
[1] => Array
(
[user_id] => 1
[entry_id] => 1
[entry_name] => twkla nnn xxx
[entry_body] => this is just UPDATED
[status] => active
[created_timestamp] => 2016-12-04 00:00:00
[updated_timestamp] => 2017-01-03 00:00:00
[id] => 2
[comment] => This is another comments
[blog_id] => 1
)
)
但是,正如您所看到的,视图中缺少许多字段,因为这是执行的SQL:
SELECT `entry`.`user_id` , `entry`.`entry_id` , `entry`.`entry_name` ,
`entry`.`entry_body` , `entry`.`status` , `entry`.`created_timestamp` ,
`entry`.`updated_timestamp` , `comments`.`id` , `comments`.`comment` ,
`comments`.`user_id` , `comments`.`blog_id` , `comments`.`status` ,
`comments`.`created_timestamp` , `comments`.`updated_timestamp`
FROM `entry`
LEFT JOIN `comments` ON `entry`.`entry_id` = `comments`.`blog_id`
WHERE `entry`.`entry_id` =1
为什么显示所有字段?
答案 0 :(得分:2)
您在"条目"中有相同的列名称表以及"评论"像user_id,status等...具有相同的列名,然后将返回主表值。只需为下面的匹配列创建别名。
comments.user_id为cuser_id,comments.status为cstatus
答案 1 :(得分:1)
用这个替换你的select语句。
$this->db->select('
entry.user_id as euid,
entry.entry_id,
entry.entry_name,
entry.entry_body,
entry.status as entry_status ,
entry.created_timestamp as entry_addtime,
entry.updated_timestamp as entry_updatetime,
comments.id as comments_id,
comments.comment,
comments.user_id as comments_userID,
comments.blog_id,
comments.status as comments_status,
comments.created_timestamp as comments_addtime,
comments.updated_timestamp as comments_addtime
');