Create a query inside a function file in the Libraries folder and not in the model as suggested

Asked

Viewed 377 times

2

I need to check a certain type of permission that is in the permissions table in the MYSQL database. In this case, I will need to use a function that is inside the "Libraries" folder in a file called "functions.php".

The purpose of this function is only to check if the user has access to specific buttons within a given "view". If it shows the related buttons.

So what is the most appropriate way forward?

  1. Create a "model" and call it in my function that is inside the folder "Libraries" in a file called "functions.php".
  2. Do the query directly inside the function that is inside the "Libraries" folder in a file called "functions.php".

I confess that it is tempting to use the second option but I do not want to hurt the standard "mvc". I’ve seen many people on the Internet create queries within functions that do not belong to the model or controller.

I’d like to know your opinion.

  • I don’t know exactly how your code is but try not to violate the SOLID SRP principle.

  • The question is plausible. It may be poorly expressed, but I fully understood the user737’s doubt. The answer is very objective

  • Though I don’t understand bulhuffles codeigniter, it seems to me to be a question responsive yes.

  • 1

    Whoever rewrote my question made things worse! Even I don’t understand that there they wrote!!!!

2 answers

2

So I’ve been developing systems with the IC for a long time, and my answer would be that it depends on what you’re doing.

The first suggestion, what I use, is to return the user’s privacy at the time of login and save it in session, as the IC makes possible by the user date. I do with all the data I constantly use the user, such as email, login, nickname, language and permission.

function mountSessionUser($userData) {
//força o carregamento da instância do CI
$CI = & get_instance();

$CI->session->set_userdata(['user-data' => json_encode($userData)]);
}

Then, if there is:

$usuario = $CI->session->userdata('user-data');

It means that the user is logged in and you will have all the data of it that you logged in, without consulting the database.

Now, on the main point of the question, which is about the relationship with the database outside the models. I use yes, no problem, but I always guide my team the way we choose to use in the project.

Coherent data to be read from the database for the rendering of a page should always be in the model or already loaded, as the example of the user name. Other functions, however, that are not linked to data that will be shown or that perform tasks parallel or common to the whole system may, in my view, be in Ibraries yes. An example of this is the two libraries I use for user management.

Within the model I keep the queries responsible for rendering user lists, user friends, and so on. But the password change function, which in this case can be done via website and via API, is in the user management library, called several times in the application, not only in the user controller.

  • Thank you Caio. I ended up doing the following (as they edited and suspended, I ran out of answers and decided to follow my instincts): I kept the query in the model and made a call from it (the model) to this function like this: Function show_btn($class=NULL, $method=NULL){ $this->CI->load->model('admin/auth_m'); Return $this->CI->auth_m->show_btn($class, $method); ;}

2

I do not know if I really understand your question, but I think I understand and I will try to answer.

The ideal is to record in session this information so soon you can access it, but to have access to this information you would need to make the query and the ideal is to always do this in the Model not to hurt the MVC standard (as you put yourself).

I believe you’ll come up with better answers if you show us some of the code. It would be a query of the Mysql database itself or a query to the database of your application?

  • Thank you Zignd. I ended up doing the following (as they edited and suspended, I ran out of answers and decided to follow my instincts): I kept the query in the model and made a call from it (the model) to this function like this: Function show_btn($class=NULL, $method=NULL){ $this->CI->load->model('admin/auth_m'); Return $this->CI->auth_m->show_btn($class, $method); } .

Browser other questions tagged

You are not signed in. Login or sign up in order to post.