如何从 CodeIgniter 中的多个表中进行选择
我有三个表:(为了简单起见,我只显示相关字段)
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_name
和last_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个(或者只是有一个想法):
我假设您的
users
表中的user_id
是articles
和comments< 的外键/code> 表,因为您说过作者和评论者
user_id
存储在users
表中。在你的控制器上:
在你的模型上:
我不确定它是否有效,但这就是想法。
Try this (or just have an idea):
I assumed that
user_id
from yourusers
table is foreign key ofarticles
andcomments
table since you said that the authors and commentatorsuser_id
are stored inusers
table.On your controller:
On your model:
I'm not sure if its work but that's the idea.
我找到了一个适合我的解决方案,我将发布并尝试解释该解决方案。
我的控制器是这样的:
如您所见,我的模型中的 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:
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.