Are Codeigniter models just utility classes?

In the MVC I'm used to, model classes (usually) represent tables and the objects of these classes are row / domain objects. I do not understand in CodeIgniter why model classes seem to be just sibling utility classes. It is spelled incorrectly

$data = array('text' => 'hello'); $this->commentModel->insert($data); 

instead

 $comment = new Comment(); $comment->text = 'hello'; $comment->save(); 

Can someone explain why CodeIgniter really simulates this method and makes me feel better? (Or tell me what I can do to fix this.)

+4
source share
3 answers

Models in CodeIgniter are designed using the singleton pattern, which you are correct. Although this seems confusing to many people who are used to working with a more PHP OOP approach, there are several reasons.

The very first thing is that you can download the model only once and get it available in superglobal for use throughout the system.

This is the only real plus here, the rest are apologetic explanations.

CI was built in 2006 with PHP 4 support as a top priority.

This is just beginning to change now. EllisLab has dropped support for PHP 4 from CI 2.0, but for now it works.

You can, of course, download the model and then use any PHP 5 syntax that you like for your models.

$ this-> load-> model ('comment');

$ comment = new Comment (); $ comment-> text = 'hello'; $ Comment-> Save ();

+4
source

CodeIgniter does not include ORM.

I would suggest looking at the Doctrine, which can be easily integrated with CI: http://codeigniter.com/wiki/Using_Doctrine_with_Code_Igniter/

+3
source

You can check out my articles on how to use Doctrine (ORM) with Codeigniter.

Part 1 of 11: http://www.phpandstuff.com/articles/codeigniter-doctrine-from-scratch-day-1-install-and-setup

+1
source

Source: https://habr.com/ru/post/1332374/


All Articles