Creating a system where users can Private Message themselves

@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.

While i’m not Tom nor did I generate his methodology, presumably $this->messagesTable->find returns an array.

I’m not sure why you would want to do that, given that a user who has sent themselves a PM would have ‘received’ that message also. But lets assume you want to anyway.

Based on the formulation i can see here (and without knowing the definition of the find() function), I would assume that you can just
find('sender', $user->id, 'sentDate DESC', 10, $offset);
and then array_merge the two resultant arrays together. You may need to do some sorting as well to get them in proper order.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.