MVC Call model from another entity

4

I'm working with CodeIgniter and as you know PHP is quite free and lets us do many things, sometimes bad practices.

The question is more theoretical than the Framework. In an MVC architecture, is it bad practice to call the model of another entity? Ex:

    Controller:
    - Garantia
    - Novedad
   Modelo
    - Model_Garantia
    - Model_Novedad

That is, calling from the Guarantee Controller the Novelty model would comply with the SOLID, POO and MVC principles? Or should I create a Novelty type object and search for what I need from the Model_Novedad?

    
asked by Fer 11.04.2018 в 22:14
source

2 answers

0

is not bad practice to consult other models, in a project you have to make several queries to bring foreign data a serious example: the order and sale where you have to bring data such as articles and customers. what if it has to be well standardized, in a mysql table in a single controller with the same name to avoid confusion call several models

$this->load->model(array(
            'MunicipiosModel',
            'DepartamentosModel'
        ));  
    
answered by 12.04.2018 / 00:19
source
1

When implementing a RESTful web service with an MVC architecture with something basic like a CRUD, it is necessary to know that the specification indicates that the entity / model for which it was designed must be called in each of the controllers.

Based on the SOLID Principles, the first is The principle of single responsibility , which states:

  

Each module or class must have responsibility for only one part of the functionality provided by the software and this responsibility must be fully encapsulated by the class.

We can realize that by making the classes dependent, you would be generating a low cohesion and a high coupling. So when making a modification to one would directly affect the other class.

The main problem with this RESTful approach is that if you have two Models one called Post and another Comment indicating the specification to be able to call a Post with all The associated Comments would be as follows:

GET /post/1
{
  "title": "My Post",
  "author": { 
    "firstName": "Angel",
    "lastName": "Oropeza"
  },
  "comments": [1, 2, 3, 4]
  // ... more fields here
}

GET /comment/1
{
  // ... more fields here
}
...
GET /comment/4
{
  // ... more fields here
}

That is, you must make a call to get the Post with id 1 and then make a call to each of the comments associated with this Post.

Finally, mention that some pages have migrated their REST services to GraphQL as GitHub. You can find more information about Here .

    
answered by 12.04.2018 в 00:26