简体   繁体   English

使用Active Record在CodeIgniter中加入SQL

[英]SQL join in CodeIgniter with Active Record

I'm trying to wrap my head around this, but I seem to go in circles. 我试图绕过这个,但我似乎是围成一圈。 I'm trying to list a users topics one by one, with the quotes belonging to that specific topic underneath. 我正在尝试逐个列出用户主题,下面是属于该特定主题的引号。 If that makes sense. 如果这是有道理的。

I have 3 tables, like so: 我有3个表,如下所示:

[USERS] user_id username [USERS] user_id用户名

[TOPICS] topic_id user_id topic_name [TOPICS] topic_id user_id topic_name

[QUOTES] quote_id topic_id quote_name [QUOTES] quote_id topic_id quote_name

I want to be able to do something like this in my view: 我希望能够在我看来做这样的事情:

Username: Thomas 用户名:托马斯

Topic 1: Whatever 主题1:无论如何

Quotes: One quote, another quote, and a third quote, all belonging to Topic 1. 行情:一个引用,另一个引用和第三个引用,都属于主题1。

Topic 2: Another topic from Thomas 主题2:托马斯的另一个话题

Quotes: Yes indeed, Okay thanks, I love Stack Overflow, These quotes belong to Topic 2. 行情:是的,好的,谢谢,我喜欢Stack Overflow,这些引用属于主题2。

But I can't get it to work, I've been trying everything, including weird stuff like: 但我不能让它工作,我一直在尝试一切,包括奇怪的东西,如:

public function get_quotes()
{

    $this->db->select('*');
    $this->db->from('topics');
    $this->db->join('quotes', 'topic_id = quote_id');

    $query = $this->db->get();

    if($query->num_rows() > 0)
    {
        foreach ($query->result() as $row) {
            $data[] = $row;
        }
    }
    return $data;
}

Is this strange, should I instead try using 'where' instead? 这有点奇怪吗,我应该尝试使用'where'代替吗? Something like: 就像是:

$this->db->where('user', $user_id);
$this->db->where('topic', $topic_id);
$this->db->where('quote', $quote_id);

I really appreciate any help I can get, or just a finger pointed in the right direction! 我真的很感激我能得到的任何帮助,或只是一个手指指向正确的方向!

Right off the bat I would ask "What is not working?" 马上我会问“什么不起作用?” , secondly I would suggest you run the profiler to show you the EXACT SQL being generated, so that you can make a valid assessment of where the ACTIVE QUERY is failing you. ,其次,我建议您运行探查器,以显示正在生成的EXACT SQL ,以便您可以有效评估ACTIVE QUERY失败的位置。

To use the profiler, stick this into your controller: 要使用分析器,请将其粘贴到控制器中:

$this->output->enable_profiler(TRUE);

It will result in a nice output of all DB calls, all POST vars, etc; 它将导致所有数据库调用,所有POST变量等的良好输出;
Reference here: http://codeigniter.com/user_guide/libraries/output.html 参考此处: http//codeigniter.com/user_guide/libraries/output.html

UPDATE UPDATE

So to fully do what you want, you need a query that returns the following columns: 因此,要完全执行您想要的操作,您需要一个返回以下列的查询:

user_id, username, topic_id, topic_name, quote_id, quote_name

Here is the active query you want (you can also use method chaining if that is clear enough): 这是您想要的活动查询(如果足够清楚,您还可以使用方法链接):

$this->db->select('u.user_id, u.username, t.topic_id, t.topic_name, q.quote_id, q.quote_name');
$this->db->from('users u');
$this->db->join('topics t', 't.user_id = u.user_id'); // this joins the user table to topics
$this->db->join('quotes q', 'q.topic_id = t.topic_id'); // this joins the quote table to the topics table
$query = $this->db->get();

Your result set will then be something like: 您的结果集将类似于:

user_id     | username  | topic_id  | topic_name    | quote_id  | quote_name
1           |Thomas     |1          |Whatever       |1          |One quote, anot...
2           |Ryan       |4          |Another...     |6          |To be or not to...

Once you have that result set, simply loop through the data to output it, and check to see if you have multiple quotes from the same person (say sort by user_id and do a test on the 2nd loop if its the same person, otherwise output the new users name). 一旦你有了结果集,只需循环遍历数据输出它,并检查你是否有来自同一个人的多个引号(比如按user_id排序,如果是同一个人,则在第二个循环上进行测试,否则输出新用户名)。

If you want all of the quotes for a specific user: 如果您想要特定用户的所有引号:

$this->db->join('TOPICS t', 'u.user_id on t.user_id')
         ->join('QUOTES q', 't.topic_id on q.topic_id')
         ->where('u.user_id', $userId)
         ->get('USERS u');

// I always echo my queries when developing to make sure they are what i'm expecting
echo $this->db->last_query();

If you want all of the quotes for all of the users 如果您想要所有用户的所有引号

$this->db->join('TOPICS t', 'u.user_id on t.user_id')
         ->join('QUOTES q', 't.topic_id on q.topic_id')
         ->get('USERS u');

echo $this->db->last_query();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM