@TomB and others, am extending my forum app to allow users to pm themselves and using the patterns thought in your book(I don’t know the patterns used in your book), I created this
//Entity class
<?php
class Message{
public $id;
public $sender;
public $receiver;
public $content;
public $sentDate;
public $readDate;
public $status;
private $user;
private $usersTable;
public function __construct(DatabaseTable $usersTable){
$this->usersTable = $usersTable;
}
public function getSender(){
if(empty($user)){
$this->user = $this->usersTable->findById($this->sender);
}
return $this->user;
}
public function getReceiver(){
if(empty($user)){
$this->user = $this->usersTable->findById($this->receiver);
}
return $this->user;
}
}
And in the controller, created this method(function as PHP calls it). inside the Message controller
public function list(){
$user = $this->authentication->getUser();
$page = $_GET['page'] ?? 1;
$offset = ($page-1)*10;
$messages = $this->messagesTable->find('receiver', $user->id, 'sentDate DESC', 10, $offset);
$title = 'Inbox messages';
return [
'template' => 'listmessages.html.php',
'title' => $title,
'variables' => [
'messages' => $messages,
'user' => $user
]
];
}
Now, what am able to retrieve is only received messages alone which exclude the sent one. How then can I go about it to make it retrieve both the sent message and retrieve message.