Welcome Guest, Not a member yet? Register   Sign In
Correct way to call the Model
#1
Sad 

Hi,

I have 2 Controllers and 1 Model:
[Controllers]
Employee
User

[Model]
UserModel
id
name
is_employee


An User is all the people registered in the app. An Employee is an User with is_employee == true

--//--

When i need an "simple" user, i use(inside User Controller)):

$this->userModel->getUser($id)

When i need all users:

$this->userModel->getAll()

--//--

When i need an employee i use (inside Employee Controller):

$this->userModel->select('id,name')->where('is_employee',true)->findAll()

Is this ok? is this the right way to do this?

Or i must have a method inside User to get the employee? Should Employee controller have direct access to UserModel ?

Pls, help. this is driving me insane!!!

Sad
Reply
#2

$mymodle = new Mymodel() ;
Enlightenment  Is  Freedom
Reply
#3

make class for Employee to manage all logic for employee. just call class instance for Employee in controller (clean code in controller)
example: Employee.php

put file in libraries folder.
put namespace at top of file:
namespace App\Libraries;
.. put all function and logic here.

in controller call:
use App\Library\Employee

so it usable on any controller later
Reply
#4

PHP Code:
// Create a new class manually
$userModel = new \App\Models\UserModel();

// Create a new class with the model function
$userModel model('App\Models\UserModel'false);

// Create a shared instance of the model
$userModel model('App\Models\UserModel');

// Create shared instance with a supplied database connection
// When no namespace is given, it will search through all namespaces
// the system knows about and attempt to located the UserModel class.
$db db_connect('custom');
$userModel model('UserModel'true$db); 

It's up to you.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

(This post was last modified: 02-16-2022, 11:12 PM by SalinaSJames.)

As long as you maintain the separation of concerns between M,V and C, you are free to call upon the Model (or the Controller) from the View. Most MVC diagrams show a bidirectional connection at least between View and Model. What you don't want to do though, is place logic/code from the Model (or the controller) into the View and you don't want to modify the model from there.

For example, you might have a widget on your page that aggregates the latest ten blog posts headlines from your favorite blogs on each page of your website. You get the headlines by calling, say MyFavFeeds::getLatest(); in your model. What are your options now?

You could add the code to fetch the headlines into the controller, but that would require you to replicate it in each and every controller action, which is against the DRY principle. Also, the controller's concern is handling user input for specific actions and fetching the headlines on each call is likely not even related to these actions.
If your architecture supports it, you could fetch that data in some sort of preDispatch hook, that is, the headlines get loaded and injected into the View from a plugin or callback. That would be DRY, but a second developer might not be aware of that plugin and accidently overwrite the variable holding the headlines from his controller action. And there might be cases in which you wouldn't want to load the headlines, e.g. when just rendering confirmation pages for form submissions, so you'd have to have mechanism for disabling the plugin then. That's a lot to consider.
You place the call to (not the code of) MyFavFeeds::getLatest() into the View or Layout template or, better, a ViewHelper, that encapsulates the call to your model class and renders the widget. This way you don't have to worry about overwriting any variables or repetition. And when you don't need the headlines on your view, you simply don't include it.
About your other question:

In my View, I'm calling a Model that runs my authentication system, and requesting the login status of a user. I then use that boolean to decide whether to show certain elements within the view, and where to place others.

Authentication is something you will want to do early in the application flow, before any controller actions are called. Thus, you should not run your (entire) authentication system in the View. The actual authentication is not View-related logic. Just requesting the user status after authentication, on the other hand, is okay. For instance, if you want to render a widget showing the user name and giving a login/logout button, it would be fine to do something like

<?php //UserHelper
class UserMenuHelper
{
    public function getUserMenu()
    {
        $link = '<a href="/user/logout">Logout</a>';
        if(MyAuth::userHasIdentity()) {
          $link = sprintf('<a href="/user/logout">Logout %s</a>',
                            MyAuth::getUsername());
        }
        return $link;
    }
}

If you got larger portions of your GUI to be modified by a User's role, you might want to break your View apart into partial blocks and include them based on the status, instead of writing all the HTML into a View Helper.

If you are only looking to render a navigation based on the user role, have a look at Zend Framework's omegle Zend_Navigation and shagle Zend_Acl to see how they do it.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB