0

We are looking at adding DataMapper ORM into our Code Igniter HMVC setup.

DataMapper looks very cool, but I noticed that the models are all tightly coupled with the has_one and has_many E.g. from the datamapper docs:

class User extends DataMapper {

    var $has_many = array('book');
    var $has_one = array('country');

........

class Country extends DataMapper {

    var $table = 'countries';

    var $has_many = array('user');

........

class Book extends DataMapper {

var $has_many = array('user');

As I mentioned at the start, we are using HMVC to deal with modulized code. This means that we may have the countries module, and/or we may have the books module. but they are modulized, so the users module (which includes the User object) doesn't know that the other two modules exist so we can't specify in the User model the information about the other two models.

Now the other two models depend on the users module, so they know it exists, What I was thinking is that in the Country model we could append book to the User has_many array?

Is this the right way to do this? I imagine datamapper must have a way to handle these use cases?

Hailwood
  • 89,623
  • 107
  • 270
  • 423

1 Answers1

0

If you can't commit your relations inside your code files, you could use Runtime relationship definition. This way you can set up relations after the model instances created, so for example in your Books module, when you get an User instance just add the "yourself" (the books) relation.

I'm not sure if this setup would be feasible in your situation. You might want to create some factory classes or methods for this so you don't have to litter your controllers with setup code.

complex857
  • 20,425
  • 6
  • 51
  • 54