Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object Oriented Approach in Codeigniter Model

I have been wondering what is the right way to write code in OO style in model. Certainly you can have a function that retrieve data from DB and then map to model-level variables. This approach, however, becomes counterintuitive when you have other function in model trying to get other data from BD. For example:

class User extends CI_Model {
    $id, $name, $age .... ;

    public function get_user_from_db_with_id($id) {
        ...
        // get data and map to variable. 
    } 

    public function get_all_users() {
    // return all users in db
    }

}

somewhere in controller:

$user = new User();
$ben = $user->get_user_from_db_with_id($id);

// this does not make sense!! 
$all_user = $ben->get_all_users();

Any thought or comment?

Thanks in advance!

like image 909
Troy Avatar asked Jan 16 '14 22:01

Troy


People also ask

Is CodeIgniter object-oriented?

It is object-oriented.

What is object-oriented approach in programming?

Object-oriented programming (OOP) is a computer programming model that organizes software design around data, or objects, rather than functions and logic. An object can be defined as a data field that has unique attributes and behavior.

What are the 4 basic methods in object-oriented programming?

Now, there are four fundamental concepts of Object-oriented programming – Inheritance, Encapsulation, Polymorphism, and Data abstraction.

What is object-oriented approach in C++?

OOP stands for Object-Oriented Programming. Procedural programming is about writing procedures or functions that perform operations on the data, while object-oriented programming is about creating objects that contain both data and functions.


2 Answers

I had to make a similar decision and opted for this (trimmed for clarity)

class UserModel extends MY_Model 
{

    public $UserID = 0;
    public $CustomerID = null;
    public $FirstName = '';
    public $LastName = '';
    public $EmailAddress = '';
    public $Password = null;
    public $UserGroupID = true;     

    function __construct()
    {
        parent::__construct();
    }

    private function get($id)
    {
        $row = $this->get($id);
        if ($row !== null)
        {
            $this->dbResultToObject($row, $this);
        }
    }

    // Return an array of User objects
    public function get_list($deleted = false, $userIDToInclude = null) 
    {
        $params = array(null, $deleted, $userIDToInclude);
        $query = $this->db->call("spUserGet", $params);

        $users = array();
        foreach ($query->result() as $row)
        {
            $user = new UserModel();
            $this->dbResultToObject($row, $user);
            $users[] = $user; 
        }

        return $users;        
    }

    // Other Methods (Validate, Save etc)
}

I use a mixture of public, protected and private properties so that the reflection code I've written to map the properties from the DB results and to the DB sproc calls then only includes the public properties and prevents too many parameters being sent. But that's getting off-topic so my controller then just looks like:

class Users extends MY_Controller 
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model('UserModel', 'user');
    }

    ....

}

Then a list can be retrieved with

$users = $this->user->get_list();

And a single record with

$user = $this->user->get($userID);
like image 126
Dazz Knowles Avatar answered Oct 09 '22 04:10

Dazz Knowles


i'm a big fan of thinking about the design in terms of "roles" - not resources. so a "User" is going to be able to get their own Profile. but its only going to be an "Admin" who is able to get All User Profiles.

so that distinction and important separation between what a User can do - get one record - and what an Admin can do - get all records - starts by having separate controllers for each. The User Controller methods are based upon verifying a single user and granting them access to one record. The Admin Controller methods are based upon verifying an admin and granting them access to all records. And this makes sense even from a URL design standpoint - you want your admin area clearly separate.

This separation at the controller makes everything simpler and easier. When you are resource oriented you are constantly checking the credentials in every method and even in your views. Views should be as simple as possible and not be tasked with "is this person an admin"? When you are role oriented - you check the credentials in the controller - and then your methods, the models, and the views are appropriate for what that 'role' needs to accomplish.

like image 25
cartalot Avatar answered Oct 09 '22 05:10

cartalot