Jump to content

Opinions - mvc


glenelkins

Recommended Posts

say i have a model called messageModel wich will have to begin with 2 functions "getInbox ( $userID)" and "getSent ( $userID )"  - i think its quite clear what they are meant to do.

 

So lets say we call the model function $_messageModel->getInbox ( 1 ) will get the inbox of user id of 1, and return an array of the database table like this $messageArray[MESSAGE_ID_HERE] = array ( message variables here - subject - content etc )

 

Now, in the message table there is a "receivedfromuserid" field. I have another model that gathers a users information.

 

What are peoples opinions on the best way to link the 2 models so the message sender can be linked to the "receievedfromuserid" ?? Call the user model from the message model? Parse a copy of the user model to the message model?

 

 

Link to comment
Share on other sites

Not sure what that has to do with the MVC pattern....

 

 

 

But anyway, one option would be to have a Message object with attributes from the table and have access methods.

 

 

Then you could lazily create a User object if the Message->getFromUser() method was called.

 

Basic layout:

 

class Message {

 

    private $data = array();

 

    public function __construct($data) {

        $this->data = $data;

    }

 

    public function getFromUser() {

        if(!isset($this->data['from_object'])) $this->data['from_object'] = new User($this->data['receivedfromuserid']);

        return $this->data['from_object'];

    }

 

}

 

 

 

If you wanted to get really fancy with it you could even have a lazy registry for User objects that would basically force the script to only have 1 user object per user id at a time (everything would be references to the object stored inside the registry).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.