CodeIgniter Forums
better approach to work with models - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: better approach to work with models (/showthread.php?tid=79549)



better approach to work with models - tino - 07-01-2021

this may seems a stupid question, and probably it is.
let's say i have to print all the users who are verificated, would be better doing something like this:
PHP Code:
class UserController extends BaseController {

    public function showEveryUser() {
        $userModel = new UserModel();

        $users $userModel->where("is_verificated"1)->findAll();

        ...
    }


or creating a specific method in the UserModel like this:
PHP Code:
class UserModel extends Model {

    public function getEveryUser() {
        return $this->where("is_verificated"1)->findAll();
    }


and then calling UserModel->getEveryUser() in UserController->showEveryUser()?


RE: better approach to work with models - includebeer - 07-01-2021

There's no right or wrong answer. It depends on how you want to organize your application. Personally I prefer to have the least possible logic in the controllers and in the models. I prefer to keep them simple and put most of the logic in a library. If your application is super small, it may be overkill to do it this way. But if you have a somewhat big application, it's easier to develop and to maintain when the logic is not everywhere in the controllers, the models, etc. It's also easier to reuse those functions if you need them in multiple places in your code. You can see an example of this in my tutorial for a basic application.


RE: better approach to work with models - ikesela - 07-01-2021

i dont do query on controller, all put in other class , more efficient (sometime same query need in different controller)


RE: better approach to work with models - [email protected] - 07-01-2021

(07-01-2021, 11:16 AM)tino Wrote: this may seems a stupid question, and probably it is.
let's say i have to print all the users who are verificated, would be better doing something like this:
PHP Code:
class UserController extends BaseController {

    public function showEveryUser() {
        $userModel = new UserModel();

        $users $userModel->where("is_verificated"1)->findAll();

        ...
    }


or creating a specific method in the UserModel like this:
PHP Code:
class UserModel extends Model {

    public function getEveryUser() {
        return $this->where("is_verificated"1)->findAll();
    }


and then calling UserModel->getEveryUser() in UserController->showEveryUser()?


In MVC format always use Queries in Model Section that is better practice. (We can reuse the code anywhere in the project by calling that particular model)


RE: better approach to work with models - superior - 07-02-2021

I would personally also put it in a Model, running queries inside a controller is a no-go for me..


RE: better approach to work with models - tino - 07-02-2021

thanks for all the answers :)