Welcome Guest, Not a member yet? Register   Sign In
Preferred model approach - instantiated or not?
#1

[eluser]tunesmith[/eluser]
Hi, I've been going back and forth between whether to do one of two different approaches with my models, and I'm a little stuck recognizing which is best given CI's structure.

Approach #1: Models have attributes refelecting database columns, and get instantiated in the control layer. Control layer sets attributes and has the model object call routines on itself. Some pseudo-code.

Controller:
Code:
$this->load->model('UserModel');
$user = new UserModel;
$user->FirstName = "Flibberty";
$user->LastName = "Bidget";
$user->insert();   // can return boolean for success/failure
echo $user->ID;

Model:
Code:
<?
class User extends Model {

var $ID;
var $FirstName;
var $LastName;

function insert ()
{
  // calls active record stuff, referring to $this->FirstName, etc.  
  // Set $this->ID to last_insert_id afterwards.
}

What I like about this approach is that it's the first approach I learned in other languages and frameworks. It's also a little more self-documenting in that you can see the database columns in the class's attributes. And it feels concise, I like writing $user->isLoggedIn() and $user->hasPermission($perm). What I don't like is that it feels a little more awkward to have static "list" methods in it like getUsersByFirstName($fname). One approach I maintained had a convention of having a sister *List class for any kind of method that wasn't appropriately called by an instantiated object. Like, UserList::getUsersByFirstName.

The other problem is that since User extends Model, each instantiated object contains a whole lot of extra stuff besides the database column information. It would be common to have a large array of User objects in other frameworks, but here it feels wasteful.

When I started learning CI I didn't instantiate and tried to use the object loaded from $this->load, but my models still had attributes reflecting db columns. I quickly ran into problems trying to loop multiple Users, because you couldn't unset a model easily.


The other approach seems to be more along the lines of:

controller:
Code:
$this->load->model('UserModel');

$array['FirstName'] = "Goofus";
$array['LastName'] = "Gallant";

$id = $this->UserModel->insert($array);

Accompanying it is a much sparser model that doesn't bother itself with database column attributes. Advantages are less typing in the model, you just sling arrays around. Disadvantages are that it's less self-documenting, and there's less flexibility in extending models, like extending a type of user. None of the methods really operate on "themselves", in that the object is just the instantiation of the CI model class with some table-specific routines, rather than an instantiation of a database table representation (meaning, the object doesn't have attributes reflecting database columns).

What is really the best approach here? Is the "CI way" the latter approach, or are there ways to code the former approach without running into some of the pitfalls? I've been considering switching from the former to the latter, but I'm not sure if I'll regret it.


Messages In This Thread
Preferred model approach - instantiated or not? - by El Forum - 11-03-2009, 07:18 PM
Preferred model approach - instantiated or not? - by El Forum - 11-03-2009, 07:45 PM
Preferred model approach - instantiated or not? - by El Forum - 11-03-2009, 09:13 PM
Preferred model approach - instantiated or not? - by El Forum - 11-03-2009, 09:57 PM
Preferred model approach - instantiated or not? - by El Forum - 11-03-2009, 10:56 PM



Theme © iAndrew 2016 - Forum software by © MyBB