ConversationModel::get2 PHP Method

get2() public method

Get a list of conversations for a user's inbox. This is an optimized version of ConversationModel::get().
public get2 ( integer $UserID, integer $Offset, integer $Limit )
$UserID integer The user looking at the conversations.
$Offset integer Number to skip.
$Limit integer Maximum to return.
    public function get2($UserID, $Offset = 0, $Limit = 0)
    {
        if ($Limit <= 0) {
            $Limit = c('Conversations.Conversations.PerPage', 30);
        }
        // The self join is intentional in order to force the query to us an index-scan instead of a table-scan.
        $Data = $this->SQL->select('c.*')->select('uc2.DateLastViewed')->select('uc2.CountReadMessages')->select('uc2.LastMessageID', '', 'UserLastMessageID')->from('UserConversation uc')->join('UserConversation uc2', 'uc.ConversationID = uc2.ConversationID and uc.UserID = uc2.UserID')->join('Conversation c', 'c.ConversationID = uc2.ConversationID')->where('uc.UserID', $UserID)->where('uc.Deleted', 0)->orderBy('uc.DateConversationUpdated', 'desc')->limit($Limit, $Offset)->get();
        $Data->datasetType(DATASET_TYPE_ARRAY);
        $Result =& $Data->result();
        // Add some calculated fields.
        foreach ($Result as &$Row) {
            if ($Row['UserLastMessageID']) {
                $Row['LastMessageID'] = $Row['UserLastMessageID'];
            }
            $Row['CountNewMessages'] = $Row['CountMessages'] - $Row['CountReadMessages'];
            unset($Row['UserLastMessageID']);
        }
        // Join the participants.
        $this->joinParticipants($Result);
        // Join in the last message.
        Gdn_DataSet::join($Result, array('table' => 'ConversationMessage', 'prefix' => 'Last', 'parent' => 'LastMessageID', 'child' => 'MessageID', 'InsertUserID', 'DateInserted', 'Body', 'Format'));
        return $Data;
    }

Usage Example

 /**
  *
  */
 public function popin()
 {
     $this->permission('Garden.SignIn.Allow');
     // Fetch from model
     $Conversations = $this->ConversationModel->get2(Gdn::session()->UserID, 0, 5)->resultArray();
     // Last message user data
     Gdn::userModel()->joinUsers($Conversations, array('LastInsertUserID'));
     $this->EventArguments['Conversations'] =& $Conversations;
     $this->fireEvent('beforeMessagesPopin');
     // Join in the participants.
     $this->setData('Conversations', $Conversations);
     $this->render();
 }