jasonhinkle / phreeze

Phreeze Framework for PHP
http://phreeze.com/
GNU Lesser General Public License v2.1
377 stars 166 forks source link

Updating a table, instead of saving #51

Open xtrasmal opened 11 years ago

xtrasmal commented 11 years ago

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:

#MessagesController 

    public function SingleView()
    {
        require_once 'Model/MessageViewed.php';
        require_once 'Model/MessageViewedCriteria.php';

        $messageId = $this->GetRouter()->GetUrlParam('id');
        $accountId = $this->GetCurrentUser()->Id;

        $criteria = new MessageViewedCriteria();
        $criteria->MessageId_Equals = $messageId;
        $criteria->AccountId_Equals = $accountId;

        $viewedMessages = $this->Phreezer->Query('MessageViewed', $criteria)->ToObjectArray();

        if($viewedMessages!= null){
            # update an existing database entry if there is one.
            foreach ($viewedMessages as $viewed) {
                $viewed->MessageId = $messageId;
                $viewed->AccountId = $accountId;
                $viewed.Update();
            }
        } else{
            # create a new database entry when there isn't one.
            foreach ($viewedMessages as $viewed) {
                $viewed->MessageId = $messageId;
                $viewed->AccountId = $accountId;
                $viewed.Save();
            }       
        }

    }

Is this the way to go?

jasonhinkle commented 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();  
        }

    }
xtrasmal commented 11 years ago

Great stuff. I will keep that in mind in the future.