如何从 CodeIgniter 中的多个表中进行选择

发布于 2024-09-13 23:44:32 字数 1500 浏览 5 评论 0原文

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

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 表执行相同的操作。

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

我如何进入同一查询中的 users 表(不同的表)并获取作者的实际 first_namelast_name 并返回它?

这是我的代码:

Controller:

$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 表中获取用户名。当然,我不必再执行另一个单独的函数调用并将更多信息传递到视图中。这里的任何帮助将不胜感激,并且实际上会为我打开一个世界:)

I have three tables: (to keep it simple I'll only show the relevant fields)

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

In the articles table, because an author can have multiple articles, I only store the author's user_id from the users table, and I do the same for the comments table.

My problem is when I run an ActiveRecord query in the articles_model to get all articles along with that articles associated comments, and then echo the author in my view with $row->author, I only get the author's user_id which is 1, 2, 3, etc etc.

How do I go into the users table (different table) in that same query and get the authors actual first_name and last_name and return that?

Here's my code:

Controller:

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

I have a ton of other fields, but this is an extremely stripped down version for simplicity's sake.

If I echo the above $row->author, I'm only going to get the user_id. I need to get the user's name from the users table instead. Surely I don't have to do another separate function call and pass more information into the view. Any help here would be greatly appreciated and would actually open up a world for me :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心不设防 2024-09-20 23:44:32

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

我假设您的 users 表中的 user_idarticlescomments< 的外键/code> 表,因为您说过作者和评论者 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;
}

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

Try this (or just have an idea):

I assumed that user_id from your users table is foreign key of articles and comments table since you said that the authors and commentators user_id are stored in users table.

On your controller:

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

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

On your model:

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

I'm not sure if its work but that's the idea.

蓝颜夕 2024-09-20 23:44:32

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

我的控制器是这样的:

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 语句处理了我想要完成的任务。我知道通过结合这两个函数可能有更干净、更有效的方法来做到这一点,但作为初学者,我想让它更容易理解。

希望这可以帮助其他人。

I have found a solution that works well for me which I will post and try to explain.

My Controller is like this:

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

As you can see, the getComments function in my model was the key. It's the JOIN statement that handles what I was trying to accomplish. I know there are probably cleaner and more efficient ways to do this, by combining the two functions, but as a beginner I wanted to keep it easier to understand.

Hopefully this can help others.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文