Is it correct to access a model from another model in CodeIgniter?

0

A few days ago some questions arose from a functionality that I had to implement.

  

Is it correct to invoke a model from another model?

     

Does my security affect anything?

     

Is it anti-functional?

It worked perfectly for me, but I do not know how much it might affect the proper functioning of the web application.

    
asked by Yasiel Espinosa 19.07.2018 в 17:05
source

2 answers

0

Answering your questions

Is it correct to invoke a model from another model?

If by right you mean if technically CodeIgniter allows it: Yes, from CodeIgniter 2.0 it is allowed to load models into another model. Whether it should be done or not depends on you, however CodeIgniter is an MVC framework, so it is recommended to load the models inside the controllers and then do all the business logic, therefore if you want to be strict in the use of MVC you should load them into the controller.

Does my security affect anything?

It would not have to affect unless in the model you are using bad practices in terms of SQL security, in this entry you can read something about SQL injection How to avoid SQL injection in PHP?

Is it anti-functional?

You say it yourself

  

It worked perfectly for me.

However from the point of view of MVC you should not do so.

As a personal criterion I would tell you to follow what the MVC pattern says, otherwise it would not make much sense to use the framework.

    
answered by 19.07.2018 в 22:38
0

I load all the models of the application in the autoload. And yes, I have functions of a model that invoke functions of another model to extract a data. The only thing to keep in mind is that each model has to be referring to a specific entity:

"Users_model" should have all the functions referring to users create_usuario, update_usuario, get_user_name ... but it should not have the function "get_pedidos_usuario" that would put it in the model "Orden_model" because the information you want to extract in that case is on orders and not users.

That's the only rule I use.

    
answered by 26.07.2018 в 09:44