CodeIgniter でメッセージング システムを構築しようとしています。以下のように3つのテーブルがあります。
メッセージ:
- ID
- thread_id
- message_id
- 主題
- メッセージ
- 送信済
メッセージスレッド:
- ID
- thread_id
メッセージ_参加者:
- ID
- thread_id
- to_id
- from_id
- message_id
新しいメッセージを簡単に作成でき、受信者はメッセージが受信されたことを確認できます。
問題は、返信があるときです。返信後、結果が重複します。最初のメッセージが発信者と受信者の両方から送信され、予想される 2 行ではなく 4 行が返されていることがわかります。誰が私が間違っているのか教えてもらえますか?
これが私のモデルです:
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 に設定したかどうかによって異なります。ユーザーテーブルの結合を最後のメッセージに結び付ける方法を知っている人はいますか?