8

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!

  • What do you mean by saying "I could add these to my Articles datamember, but it's better practice to seperate those"? I suppose you should extend your Article class from Model class (http://codeigniter.com/user_guide/general/models.html) and go on. – Darmen Amanbay Sep 10 '10 at 10:36
  • by that i mean adding the datamembers to the model, like on the example, but is it not better to separate entities? –  Sep 10 '10 at 11:51
  • Can you clarify what you mean by 'entities'? – user229044 Sep 10 '10 at 15:16
  • a class with datamembers and getters and setters (only) –  Sep 10 '10 at 15:38
  • It sounds like you're talking about what CodeIgniter considers a "model", in the model-view-controller paradigm – user229044 Sep 13 '10 at 16:24
  • To avoid any confusion from @maegar comment, what Sled describes is NOT a codeigniter (or MVC) 'model'. An MVC model will contain business logic and is much more than just an entity class. – charliefortune Jun 24 '13 at 12:34

3 Answers3

12

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
  • 328
  • 3
  • 15
  • 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 Sep 14 '10 at 10:13
4

I liked @ilbesculpi's solution, but I customized it a bit using a namespace rather than using a constant and require_once. Here is my version:

1) Create my entity classes in /system/application/entities.

namespace MyApplication\Entities;

class ArticlesEntity
{
    function doSomeStuff()
    {
        // Your code here...
    }
}

2) Include my entity class in my model via a using statement.

use MyApplication\Entities\ArticlesEntity;

class ArticlesModel extends CI_Model
{
    function test() 
    {
      $entity = new ArticlesEntity();
      $ArticlesEntity->doSomeStuff();
    }
}
Tod Birdsall
  • 17,877
  • 4
  • 38
  • 40
  • 1
    In my opinion, this is more elegant solution. However, I think it needs some autoloading mechanism like PSR-4. – MÇT Aug 10 '16 at 08:39
  • 1
    CodeIgniter4 will have support for working with entities: https://codeigniter4.github.io/CodeIgniter4/models/entities.html – Gfy Apr 13 '19 at 12:45
1

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
  • 30,637
  • 12
  • 78
  • 117