Codigniter链接查询。 [英] Codigniter linking queries.

查看:217
本文介绍了Codigniter链接查询。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,如果这是一个真正的n00b问题,但我只是不太可能似乎得到我的头这个问题。

I am sorry if this is a really n00b question but i just cant seem to get my head around this problem.

我使用mahana消息库,我试图创建一些基本视图和基本控制器来发送消息和检索消息。我发送了一个消息钉在(大约),我可以查看收件人配置文件上发送的消息。

I am using the mahana message library and I am trying to create some basic views and the basic controller to send a message and retrieve messages. I have sending a message nailed (just about) and I can view the sent message on the recipients profile.

这是我的问题。我想能够在收件人查看消息页面上显示发送邮件的用户的详细信息。这些详细信息存储在users表中。

This is my problem. I want to be able to show the details of the user that sent the message along side the sent message on the recipients view message page. These details are stored in the users table.

问题是,如何运行模型查询从我的消息控制器的view_my_messages函数中获取用户名,头像位置等,基于get_all_threads的结果($ user_id)模型?

The question is, how do I run a model query to get the username, avatar location etc from the view_my_messages function in my messages controller, based on the result of the get_all_threads($user_id) model in the messaging model?

我已经包含了控制器中的代码,视图和模型。

I have included the code from the controller, view and model below.

控制器:

function view_my_messages(){
        $this->load->library('mahana_messaging');
        $user_id = $this->session->userdata('id');

        $data['message'] = $this->mahana_model->get_all_threads($user_id);

        $this->load->view('messages/my_messages',$data);

    }

消息模型:

function get_all_threads($user_id, $full_thread = FALSE, $order_by = 'asc')
    {
        $sql = 'SELECT m.*, s.status, t.subject, '.USER_TABLE_USERNAME .
        ' FROM ' . $this->db->dbprefix . 'msg_participants p ' .
        ' JOIN ' . $this->db->dbprefix . 'msg_threads t ON (t.id = p.thread_id) ' .
        ' JOIN ' . $this->db->dbprefix . 'msg_messages m ON (m.thread_id = t.id) ' .
        ' JOIN ' . $this->db->dbprefix . USER_TABLE_TABLENAME . ' ON (' . USER_TABLE_TABLENAME .'.'. USER_TABLE_ID . ' = m.sender_id) '.
        ' JOIN ' . $this->db->dbprefix . 'msg_status s ON (s.message_id = m.id AND s.user_id = ? ) ' .
        ' WHERE p.user_id = ? ' ;

        if (!$full_thread)
        {
            $sql .= ' AND m.cdate >= p.cdate';
        }

        $sql .= ' ORDER BY t.id ' . $order_by. ', m.cdate '. $order_by;

        $query = $this->db->query($sql, array($user_id, $user_id));

        return $query->result_array();
    }

查看:

<div class="row-fluid">

    <div class="span12">
        <div class="well">
            <h3>View Messages</h3>
            <?php foreach($message as $messagefield):?>

            <h4><?php echo $messagefield['subject'];?></h4>

            <?php endforeach; ?>
        </div>
    </div>
 </div>

感谢您的帮助!

推荐答案

@Morph_ByteSense - 您有两个选项:

@Morph_ByteSense - you have two options:

1)msg_participants p是保存线程上所有其他人的表只是发件人),你可以加入用户表一次,再到&在你的结果中扩展你的SELECT stmt

1) msg_participants p is the table that holds all the other people on the thread (not just the sender), you you can join users table once more to that & extend your SELECT stmt

2)(不太好,但是你不需要扩展库),你可以做一个foreach查询所有参与者&查询详情

2) ( not as good, but you don't need to extend the library) in your result, you can do a foreach() loop and query over all the participants & query details

我会推荐第一个选项 - 我从来没有人要求这个,但它可能会有一个很好的增强

I would recommend the first option - I've never had anyone ask for this before, but it might make a good enhancement

这篇关于Codigniter链接查询。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆