3

I have built a small PHP MVC framework and just want to clarify the best way to get data from one model into another. For example:

I have a Users_model that contains a method called get_users().

I also have Communications_model that needs to get specific or all user data and as such needs to access the get_users() method from the Users_model.

Is it best practice to:

a) Instantiate the Users_model in a controller and pass the data from the get_users() method into the Communications_model?

b) Instantiate the Users_model inside the Communications_model and run get_users() from there, so it can be accessed directly?

c) Another way?

Many thanks for any help.

markwilliamsweb
  • 470
  • 6
  • 15

4 Answers4

0

It depends of your motive behind this.

  1. If you want effect on result, then using well know library, like Doctrine etc. should be your choice.
  2. If you want to learn design patterns, then you should get read about ActiveRecord or DataMapper + Repository patterns. Then implements both and check out.
  3. If you want your code, this way - ORM should represent relations of data, then you should ask what it more important? If you menage communication (bus, train), then user can be there assigned and getting users from communication is OK. If user have communication (like car), then relation is reversed.

All depends, what is you motive behind this. Using library, like Doctrine, could you help you running you application. If you want learn design patterns, then check out both options to get some experience.

timiTao
  • 1,417
  • 3
  • 20
  • 34
0

What you call "users model" is a repository. And what you call "communication model" looks like a service.

Your communication service should have the user repository passed in constructor as a dependency.

I honestly think, that a huge part of your confusion is that you try to call all of those things "models". Those classes are not part of the same layer. You migth find this answer to be useful.

Community
  • 1
  • 1
tereško
  • 58,060
  • 25
  • 98
  • 150
-1

All are possible ways but what I usually do is, whenever there is any function that I think would be reused a number of times by a number of objects, I declare it as static.

It would save the effort of playing with object declaration and would be easily accessible by ClassName::function();

Again, it's a design choice, usually objects are declared right there in the controller and used as per the need but just to save declaration of objects again and again I follow the approach of declaring function static.

Danyal Sandeelo
  • 12,196
  • 10
  • 47
  • 78
  • 1
    By design, the reason for you're using a static function is wrong. A static, or not, function should be created by context, if the class is stateful or stateless and a few more reasons. In 99,9% of cases, a model class is stateful which means that should exist an instance. – Gabriel Heming May 12 '17 at 14:22
  • that's good, by design i mean based on my requirements and design. Usually utilities are declared with abstract classes and the functions are kept static since we are sure about the features to be offered by a particular utility – Danyal Sandeelo May 12 '17 at 14:25
-2

The simple principle here is using the __construct() (constructor) to build the object with the relevant properties from the Database. The User Model will have a static function (therefore accessible through any scope) to create an array of instanced objects by simply passing the model data through a new self() which returns the instance.

The concept is you end up with an array of User_Model instances each being a build of the Database columns to properties. All that's left is to create the Database Model and the functions to retrieve the columns and data.

class Communications_Model {
    private $_all_users;
    public function getUsers() {
        $this->_all_users = Users_Model::loadAllUsers();
    }
}

class Users_Model {
    private $_example_property;
    public function __construct($user_id) {
        $data = SomeDatabaseModel::getConnection()->loadUserFromDatabase((int)$user_id);
        $this->_example_property = $data['example_column'];
    }
    public static function loadAllUsers() {
        $users = array();
        foreach(SomeDataModel::getConnection()->loadAllUsers() as $data) {
            $users[] = new self($data['user_id']);
        }
        return $users;
    }
}

Of course, now, you have a $_all_users; property that has an array of instanced User Models containing the data.

Jaquarh
  • 6,493
  • 7
  • 34
  • 86