glenelkins Posted August 8, 2009 Share Posted August 8, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/169391-opinions-mvc/ Share on other sites More sharing options...
corbin Posted August 9, 2009 Share Posted August 9, 2009 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). Quote Link to comment https://forums.phpfreaks.com/topic/169391-opinions-mvc/#findComment-893880 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.