2013-08-31 130 views
3

我想在CodeIgniter中構建一個消息系統。我有三個表如下。Codeigniter奇怪的查詢結果

消息:

  • ID
  • THREAD_ID
  • MESSAGE_ID
  • 受試者
  • 消息
  • 發送

messages_thread:

  • ID
  • THREAD_ID

messages_participants:

  • ID
  • THREAD_ID
  • to_id
  • from_id
  • message_id

我很容易編寫新消息,並且收件人可以看到收到消息。

問題出現時,有答覆。答覆結果重複。它表明最初的消息是從發起者和接收者發送的,並且返回4行而不是預期的2行。誰能告訴我我要去哪裏?

這裏是我的模型:

function reply_to_thread(){ 
    //Generate random string for the ticket number 
    $rand = substr(str_shuffle(MD5(microtime())), 0, 11); 
    //Add the random string to the hash 
    $messageid = $rand; 
    $messageinsert = array(
     'message_id' => $messageid, 
     'subject' => $this->input->post('subject'), 
     'message' => $this->input->post('message'), 
     'thread_id' => $this->input->post('thread_id'), 

    ); 

    $participantsinsert = array(
     'message_id' => $messageid, 
     'from_id' => $this->input->post('from_id'), 
     'to_id' => $this->input->post('to_id'), 
     'thread_id' => $this->input->post('thread_id'), 
    ); 
    $this->db->insert('messages',$messageinsert); 
    $this->db->insert('messages_participants',$participantsinsert); 
} 

function view_thread($thread_id){ 
    $this->db->where('messages.thread_id', $thread_id); 
    $this->db->join('messages_participants', 'messages_participants.thread_id = messages.thread_id'); 
    $this->db->join('users', 'messages_participants.from_id = users.id'); 
    $result = $this->db->get('messages'); 
    var_dump($result); 
    return $result->result_array(); 
} 

我的控制器:

function check_messages(){ 
    $id = $this->session->userdata('id'); 
    $data['thread'] = $this->message_model->check_messages($id); 
    $this->load->view('messages/my_messages', $data); 

} 
function view_thread($thread_id){ 
    $data['thread'] = $this->message_model->view_thread($thread_id); 
    $this->load->view('messages/view_thread',$data); 
} 
function reply(){ 
    $this->message_model->reply_to_thread(); 
    redirect('messages/get_messages'); 
} 

我的看法線程視圖:

<div class="well"> 
    <h3>View Messages</h3> 
    <?php foreach($thread as $threadfield):?> 

    <div class="message"> 
    <img class="avatar pull-left" src="<?php echo $threadfield['avatar'];?>"> 
    <div class="message-actions"> 
     <button class="btn btn-success" id="reply">Reply</button> 
    </div> 
    <p> 
     <strong> 
     From: <a href="profile.html"> Users id: <?php echo $threadfield['from_id'];?></a> 
     </strong> 
     <span class="badge badge-important">Unread</span><br> 
     <strong>Date:</strong> 
     <?php echo date('M j Y g:i A', strtotime($threadfield['sent']));?> 
    </p> 
    <p><strong>Subject:</strong> 
     <a href = "/messages/view_thread/<?php echo $threadfield['thread_id'];?>"> 
     <?php echo $threadfield['subject'];?> 
     </a> 
    </p> 
    <p><strong>Message:</strong> <?php echo $threadfield['message'];?></p> 
    <hr> 
    </div> 
    <?php endforeach; ?> 
</div> 
<div class="row-fluid" id="replyform" style="display: none"> 
    <div class="well"> 

    <h3>Reply to <?php echo $threadfield['username'];?>'s Message.</h3> 

    <?php echo form_open('messages/reply');?> 

    <input type="text" value="<?php echo $threadfield['thread_id'];?>" name="thread_id" id="thread_id"> 
    <input type="text" value="<?php echo $this->session->userdata('id');?>" name="from_id" id="from_id"> 
    <input type="text" value="<?php echo $threadfield['from_id'];?>" name="to_id" id="to_id"> 
    <input type="text" value="RE: <?php echo $threadfield['subject'];?>" name="subject" id="subject"> 
    <input type="text" placeholder="Message......." name="message" id="message"> 
    <button class="btn" type="submit">Submit Reply</button> 
    <?php echo form_close();?> 
    </div> 

編輯:

我設法解決了這個問題,但它創造了一個新的問題。我通過修改連接來解決這個問題,所以我們沒有綁定到用戶但是參與者的消息,但是現在我沒有辦法顯示正確的userdata來顯示最初的新消息,它總是會顯示最後發送者的詳細信息或最後收件人的詳細信息。這取決於我是否將連接設置爲from_id或to_id。有誰知道我可以如何配合用戶表加入到最後的消息?

+2

我就加爲評論,因爲我沒有時間,現在去通過你的整個問題:我有一個消息lib中笨這裏歡迎您來看看,看看我是如何解決這些問題:https://github.com/jrmadsen67/Mahana-Messaging-library-for-CodeIgniter – jmadsen

+0

@jmadsen你好,謝謝你的迴應,我確實開始使用你的庫,但我碰到類似這樣的問題,請參閱下面的問題。也許你可以幫我解釋一下嗎? http://stackoverflow.com/questions/18488642/codigniter-linking-queries –

+0

在其他線程中回答 – jmadsen

回答

0

我在您的查詢中看不到任何ORDER BY以顯示最新記錄。

function view_thread($thread_id){ 
    $this->db->where('messages.thread_id', $thread_id); 
    $this->db->join('messages_participants', 'messages_participants.thread_id = messages.thread_id'); 
    $this->db->join('users', 'messages_participants.from_id = users.id'); 
    $this->db->order_by('field_name_goes_here');//put the name of field to set order by.. 
    $result = $this->db->get('messages'); 
    return $result->result_array(); 
} 
+0

我應該通過啓用來自用戶表的數據來匹配每個單獨消息的哪個字段? –