Codeigniter奇怪的查询结果

时间:2013-08-31 23:45:03

标签: php mysql codeigniter join messaging

我正在尝试在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>

修改

我设法解决了这个问题,但它创建了一个新问题。我通过修改连接来解决它,所以我们没有绑定到用户但是参与者的消息现在虽然我无法显示正确的用户数据来显示初始新消息,但它总是会显示最后的发件人详细信息或最后的收件人详细信息。这取决于我是否将连接设置为from_id或to_id。有谁知道我如何能够将用户表连接到最后一条消息?

1 个答案:

答案 0 :(得分: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();
}
相关问题