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




Theme © iAndrew 2016 - Forum software by © MyBB