2

I'm currently designing a game that utilises CodeIgniter, but I don't think the singleton approach to models is the way I want to handle the game-related objects in the DB.

I want to use a CodeIgniter Model class to handle the basic CRUD operations of the objects, but have the objects as PHP Classes.

Example:

 $this->load->model('playermodel');
 $player = $this->playermodel->get($player_id); // Returns a Player object

 // Call modifying operations on the player (equip item, take damage etc.)

 $this->playermodel->save($player); // Commits updated Player back to DB

 $player2 = $this->playermodel->create(); // Basically calls new Player()

I'm fairly new to CodeIgniter, would something like this be going against any kind of CodeIgniter or MVC design rules? And if so, could anyone recommend me another approach to my problem?

Tom
  • 51
  • 7
  • I can't answer your design questions, however i recommend you to take a look into the [DataMapper](http://datamapper.wanwizard.eu/) library. I'm using this to get rid of codeigniter's singleton loving tendencies. – complex857 Nov 08 '12 at 14:46
  • Looks like an excellent library for what I'm trying to do, thanks! – Tom Nov 09 '12 at 01:17

2 Answers2

4

The best approach would be for you to get as far as you can from Codeigniter, if you want to build proper MVC. Codeigniter is framework written for PHP4, and it didn't get any updates for quite some time now, I'm talking about framework design, not some library updates.

If you look at Codeigniter source code and look what base model do, it just takes request from model and pass it back to the controller using magic _set and _get methods. So Codeigniter doesn't really know a difference between your models or controllers. And how every you write this, everything is processed in some mega super ultra global object.

From outside it might look like you are using MVC but you are not really. Take a look at Zend2, Symfony2... where you can really build your Models.

otporan
  • 1,313
  • 1
  • 12
  • 28
  • Thanks for the little insight into how CodeIgniter operates internally, and I'll be sure to check out those suggestions! – Tom Nov 09 '12 at 01:04
3

What you currently have is a strange interpretation of data mapper pattern (do not confused with CI's ORM with same name, which instead implements active record pattern).

The idea would be to separate the domain logic from the interaction with storage abstraction. For simplified code example, you can read this post.

Only major problem, that i sea with your implement, is that your mapper also contains logic for creation of instance. Thus, it has acquired also the characteristics of a factory (or maybe, builder .. depends on your particular use-case). This would violate single responsibility principle.

But non of this is against CI's design rules. Because it has none.

Community
  • 1
  • 1
tereško
  • 56,151
  • 24
  • 92
  • 147
  • Ok, that first link outlines exactly the structure what I was aiming for, thanks! In your opinion, would structuring the model class as the data mapper (but removing the creation logic) be valid from a design point of view? – Tom Nov 09 '12 at 01:12
  • Thing is, what people usually refer to as "models" are actually [domain objects](http://c2.com/cgi/wiki?DomainObject). In that diagram `Person` class instances would be domain objects. If I had to work with CI, I would use `$this->load->model()` to instantiate factories (kinda like [this](http://stackoverflow.com/a/11369679/727208)). Also, AFAIK, game logic is usually quite complicated therefore it might be useful for you to contain the interaction between mappers and domain objects in [services](https://en.wikipedia.org/wiki/Service_(systems_architecture)). – tereško Nov 09 '12 at 09:31