Create a query within a function file in the libraries folder and not in the model as suggested

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 to simply check if the user has access to specific buttons within a given "view". If it does it shows the related buttons.

So, what is the most appropriate way forward?

  • Create a "model" and call it in my function that is inside the "libraries" folder in a file called "functions.php".
  • 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 "mvc" pattern. I've seen a lot of people on the internet create queries within functions that do not belong to the model or controller.

    I would love to hear from you.

        
    asked by anonymous 24.12.2013 / 18:04

    2 answers

    2

    I do not know if I really understood your question, but I think I understand it and I'll try to answer it.

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

    I think you'll get better answers if you show us part of the code. Would it be a query of the MySQL database itself or would it be a query to the bank of your application?

        
    29.12.2013 / 03:21
    2

    So, I developed systems with CI a long time ago, 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 CI enables by the user data. I do with all the data that I use constantly, 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 it exists:

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

    It means that the user is logged in and you will have all the data of him that did in the login, without consulting the bank.

    Now, about 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 mentor my team the way we choose to use it in the project.

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

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

        
    28.12.2013 / 19:56