如何从CodeIgniter中的多个表中进行SELECT

时间:2010-08-11 02:46:44

标签: mysql activerecord codeigniter select join

我有三个表:(为了保持简单,我只会显示相关字段)

Articles (id, title, text, author)
Comments (id, article_id, text, author)
Users (user_id, user_type, first_name, last_name, email, password)

articles表格中,因为作者可以有多篇文章,所以我只存储user_id表中作者的users,我为comments做同样的事情。 } table。

我的问题是当我在articles_model中运行ActiveRecord查询以获取所有文章以及那些文章相关的评论,然后在我的视图中用$row->author回应作者时,我只得到作者的user_id,即1 ,2,3等等

如何进入同一查询中的users表格(不同的表格),让作者真实first_namelast_name并返回?

这是我的代码:

控制器:

$data['articles'] = $this->getArticles();
$this->load->view('articles_view', $data);

function getArticles() {
  $this->load->model('articles_model');
  $articles = $this->articles_model->getAllArticles();
  return $articles;
}

articles_model:

function getAllArticles() {
  $this->db->where('id', $this->uri->segment(3));
  $query = $this->db->get('articles');
  return $query;
}

articles_view:

<?php foreach($articles->result() as $row) { ?>
<?php echo $row->author ?>
<?php echo $row->text ?>
<?php } ?>

我还有很多其他领域,但为了简单起见,这是一个非常简化的版本。

如果我回应上面的$row->author,我只会得到user_id。我需要从users表中获取用户名。当然,我不必再进行另一个单独的函数调用,并将更多信息传递给视图。在这里的任何帮助将不胜感激,并将为我开辟一个世界:)

2 个答案:

答案 0 :(得分:1)

试试这个(或者只是有个主意):

我假设您user_id表中的usersarticlescomments表的外键,因为您说存储了作者和评论员user_idusers表中。

在您的控制器上:

$data['articles'] = $this->getArticles(); 
$this->load->view('articles_view', $data);

function getArticles() { 
   $this->load->model('articles_model'); 
   $articles = $this->articles_model->getArticlesById(); 
   return $articles; 
}

在您的模型上:

// this will get the article of a certain author   
function getArticlesById()
{
   $this->db->where('id', $this->uri->segment(3)); 
   $q = $this->db->get('articles'); 
   if($q->num_rows() > 0)
   {
       $q = $q->row_array();
       $result = $this->getCommentsAndAuthors($row);
       return $result;
   }
   return 0;
}

// this will get the comments of each articles of the author and the author of the comments
function getCommentsAndAuthors($row)
{
    $result = $data = array();

    $this->db->select('c.*, u.firstname, u.lastname');
    $this->db->from('users u');
    $this->db->where('u.id', $row['user_id']);
    $this->db->join('comments c', 'c.id = u.id');
    $q = $this->db->get();
    foreach($q->result_array() as $col)
    {
        $data[] = $col;
    }
    $result['article'] = $row;
    $result['comments'] = $data;
    return $result;
}

我不确定它是否有效,但这就是想法。

答案 1 :(得分:0)

我找到了一个适合我的解决方案,我将发布并尝试解释。

我的控制器是这样的:

function fullArticle()
{
    $data['article'] = $this->getArticleDetail();
   $data['comments'] = $this->getNewsTrendsComments();
   $this->load->view('inner_view', $data);
}

function getArticleDetail()
{
   $this->load->model('articles_model');
   $articles = $this->articles_model->getArticles();
   return $articles;
}

function getComments()
{
   $this->load->model('articles_model');
   $comments = $this->articles_model->getComments();
   return $comments;
}


articles_model:
function getArticles()
{
   $this->db->where('id', $this->uri->segment(3));
   $query = $this->db->get('articles');
   return $query;
}

function getComments()
{
   $this->db->select('*');
   $this->db->from('comments w');
   $this->db->where('article_id', $this->uri->segment(3));
   $this->db->join('users wc','w.author = wc.user_id');
   $data = $this->db->get();
   return $data;
}

如您所见,我的模型中的getComments函数是关键。这是处理我想要完成的事情的JOIN语句。我知道通过结合这两个功能,可能有更简洁,更有效的方法,但作为一个初学者,我想让它更容易理解。

希望这可以帮助其他人。

相关问题