【问题标题】:How can I SELECT from multiple tables in CodeIgniter如何从 CodeIgniter 中的多个表中进行选择
【发布时间】:2011-03-28 03:55:10
【问题描述】:

我有三个表:(为简单起见,我只显示相关字段)

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

articles表中,由于一个作者可以有多篇文章,所以我只存储users表中作者的user_idcomments表也是如此。

我的问题是,当我在articles_model 中运行ActiveRecord 查询以获取所有文章以及与该文章关联的cmets,然后在我的视图中使用$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-&gt;author,我只会得到user_id。我需要从users 表中获取用户名。当然,我不必进行另一个单独的函数调用并将更多信息传递到视图中。在这里的任何帮助将不胜感激,实际上会为我打开一个世界:)

【问题讨论】:

  • articles和cmets表中的'author'是users表的外键吗?

标签: mysql activerecord codeigniter select join


【解决方案1】:

试试这个(或只是有一个想法):

我假设您的users 表中的user_idarticlescomments 表的外键,因为您说作者和评论员user_id 存储在users 表中。

在您的控制器上:

$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;
}

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

【讨论】:

  • 实际上我正在使用不支持外键的 MyISAM 存储引擎。有什么建议?我可以将我所有的表都转换为 InnoDB,但这将是很多我希望避免的繁琐工作。
  • @JoeyDuke:不支持外键是什么意思? articles 表中的 user_id 列是外键。任何数据库都支持外键。
【解决方案2】:

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

我的控制器是这样的:

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 语句。我知道通过结合这两个功能可能有更清洁和更有效的方法,但作为初学者,我想让它更容易理解。

希望这可以帮助其他人。

【讨论】:

    猜你喜欢
    • 2011-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-22
    • 1970-01-01
    • 2021-02-09
    • 1970-01-01
    相关资源
    最近更新 更多