The simplest way would be to just create a messages
database table with at the very least, five columns: id
, sender_id
, recipient_id
, subject
, body
. You can then also add other columns you desire, such as created
.
You can then set up your controller as follows:
<?php
class MessagesController extends AppController {
public function inbox() {
$messages = $this->Message->find('all', array(
'conditions' => array(
'recipient_id' => $this->Auth->user('id')
)
));
}
public function outbox() {
$messages = $this->Message->find('all', array(
'conditions' => array(
'sender_id' => $this->Auth->user('id')
)
));
}
public function compose() {
if ($this->request->is('post')) {
$this->request->data['Message']['sender_id'] = $this->Auth->user('id');
if ($this->Message->save($this->request->data)) {
$this->Session->setFlash('Message successfully sent.');
$this->redirect(array('action' => 'outbox'));
}
}
}
}
Obviously you'll need to flesh this example out, and change anything that may not apply to your application. You'll also need to add in checks for if the user is friends with the person they're trying to message if you want that as well.