Open xtrasmal opened 11 years ago
I think you are on the right track. Here's a suggested improvement that's a little more modular. I like to add methods to my model to keep things tidy. This code assumes that for each message, the user will only need one "MessageViewed" record? In other words you're not creating a timestamp for each and every time the user views a particular message.
class User
{
public function HasViewedMessage($messageId)
{
require_once "Model/MessageViewedCriteria.php";
$criteria = new MessageViewedCriteria();
$criteria->MessageId_Equals = $messageId;
$criteria->AccountId_Equals = $this->Id;
return $this->_phreezer->Query('MessageViewed', $criteria)->Count() > 0;
}
}
#MessagesController
public function SingleView()
{
$messageId = $this->GetRouter()->GetUrlParam('id');
if (!$this->GetCurrentUser()->HasViewedMessage($messageId)){
// user has never viewed this message
$viewed = new MessageViewed($this->Phreezer);
$viewed->MessageId = $messageId;
$viewed->AccountId = $accountId;
$viewed->Save();
}
}
Great stuff. I will keep that in mind in the future.
In order to get information about Messages, that are Viewed by the User I need to update or save into the database.
I have 2 tables: Messages and MessageViewed. MessageViewed should be filled or updated when the SingleView method of the Messages controller is called. So I decided to use the following code in order to do so:
Is this the way to go?