Welcome Guest, Not a member yet? Register   Sign In
DMZ - setting model relationships outside the model
#1

[eluser]prestondocks[/eluser]
HI,

I am wanting to make my models more independent of each other while still using DMZ. The idea being that I would like to drop in new models, without having to update the current models to define new relationships.

My idea is to put in the construct of the new model a line the uses array push to define the other side of the relationship.

So I have a User model that already exists and it has the following relationships defined.

Code:
class User extends DataMapper
{
//Relationship properties
var $has_one = array('profile');
var $has_many = array();

...
//Setter function to extend the has_many relationship
public function set_has_many($class_name)
{
  array_push($this->has_many,$class_name);
}

}

I now want to create a new model called Task it will look like
Code:
class Task extends DataMapper
{
//Relationship properties
var $has_one = array('user');
var $has_many = array();

function __construct()
{
   parent::__construct();
   //This code tells the user model that it now has a relationship to task
   $this->load->model('user');
   User::set_has_many('task');
}

}

For some reason this is not working. The has_many property is being updated, as I can echo it to the screen and see the added array element.

I am wondering if when my call to set_has_many() takes place that DataMapper has already grabbed the values of the properties and changing them has no effect.

Can anyone confirm if it is possible to do this in some other way?

Thanks
Simon
#2

[eluser]WanWizard[/eluser]
Relationships are processed in the class constructor.
Also, I you have the production cache active, the information is stored and read back from the cache.

This requires quite a change to the code. If you want, you can create a new issue on http://bitbucket.org/wanwizard/datamapper.
#3

[eluser]prestondocks[/eluser]
Thanks WanWizard,

I have posted an enhancement request on the Bitbucket page for DataMapper.

By the way, how did your exploration of Java go?, I'm sure I saw a message on one site that said you were looking to hand over the rains of Datamapper to someone new.

In my opinion, all roads lead back to Codeigniter.

Thanks again
Simon
#4

[eluser]WanWizard[/eluser]
Me and Java? Don't know where you picked that up. I've just taken over from Phil, who has indeed venture into Java land. I have no intentions of quitting, on the contrary, I intent to maintain it actively.

And to prove that, I've just committed a new version to bitbucket.

This version has two new methods, has_one() and has_many(). You'll find the updated documentation here.
#5

[eluser]prestondocks[/eluser]
Hey WanWizard,

Thank you!!!!!!!!!!!!!!

Your a star, the speed of response is above and beyond.

I am going to check it out now, however the link to the updated documentation on advanced relationships is not working, but I think I saw some descriptions in the code comments when I had a quick look on BitBucket.

I will let you know how the testing goes.

Sorry about getting my maintainers mixed up, I new that someone from DataMapper had fled to the world of Java. I hope he comes back to us, it is a shame to loose someone with the skill and knowledge of codeigniter.

Thanks again
Simon
#6

[eluser]WanWizard[/eluser]
Oops, looks like I've pasted a link to my dev server in by mistake. I've updated the link.
#7

[eluser]WanWizard[/eluser]
To follow up on this question:

I've added a new method "production_cache" to the current dev version of Datamapper on BitBucket.

This method solves one of the biggest issues with the schema cache, which is that it is created at the first load of the object, and then forever used. If you make any changes, you have to manually delete the file. Now you don't have to do this, just call this new method, and the schema cache will be updated. If you do this after you have used the new has_one() or has_many() methods, these relations will be saved to the cache as well. You can also do this after you have updated your tables, p.e. using dbforge methods.

This is particulary handy in modules, where you want to update relations between core models (or models in other modules) with models a module introduces as soon as you install the module (and create the tables that it needs).




Theme © iAndrew 2016 - Forum software by © MyBB