views:

62

answers:

2

Hey,

I was wondering what should I do with my entities? For example, a class named Articles, with a few datamembers (name/title/date) and getters & setters. I could add these to my Articles datamember, but it's better practice to seperate those. So what do you think?

Thanks!

A: 

CodeIgniter models use the singleton pattern. You can create libraries or use $foo = new Some_Model if you like, remember that its all just PHP :)

Phil Sturgeon
Doesn't really work well if you want, say, a list of products. All you are doing is creating a function collection.
DisgruntledGoat
Right, CodeIgniter models are generally dumb, thin, singletons. But you can use plain old PHP to do things however you like.
Phil Sturgeon
+1  A: 

i usually do this:

1.- create my entity classes in /system/application/classes

class MyEntity {
}

2.- define a constant to point to that folder on /system/application/config/constants.php

define('CLASSES_DIR', APPPATH . "classes/");

3.- include the entities classes from the models:

require_once(CLASSES_DIR  . "MyEntity.php");

class MyModel extends Model {

   function test() {
      $entity = new MyEntity();
      $entity->doSomeStuff();
   }

}

That won't break your mvc structure, and keeps for entities classes separated. Hope that helps!

ilbesculpi
This seems like the best solution. I asked this question recently at http://codeigniter.com/forums/viewthread/166650/ but didn't get any solid replies.
DisgruntledGoat