Welcome Guest, Not a member yet? Register   Sign In
Datamapper ORM question
#1

[eluser]Unknown[/eluser]
Hi, I am developing an application usign Datamapper library. It's been really helpful so far but there's a problem with a relationship mapping I've come across.
The thing goes like this:
I have the following entities that I represent using a Datamapper model.

Team: soccer team from a league.
Match: a match that is played in a given date.
Date: a date or weekend when matches corresponding to a league round are played.

The match should have a home team, away team and a date asociated with it. However, setting up it's model with $has_one('team','team') doesn't work. I understand this is due to the fact that datamapper is designed with the idea in mind that the database scheme should be in the fifth normal form. So in order to fix this I defined to other models Home and Away both extending from the Team model and using the same table that the Team model does. However I'm almost sure this is not the best solution.
I also created a matches_teams(match_id,home_id,away_id) table in order to be able to save this relationship. Anyways, I haven't been able to save a new match object since i get the following error when i try to do so: "A Database Error Occurred You must use the "set" method to update an entry."
The code snippet where I try to do it is this:

Code:
$match = new Match();
$home= new Home();
$away= new Away();
$date = new Date();

// throws error
$match->save($home->where('id',$homeid)->get());
// throws error
$match->save($away->where('id',$awayid)->get());
// throws error
$match->save($date->where('id',$dateid)->get());
// throws error
$match->save();

All the save() calls produce the same result.

So i wonder what is it that i'm doing wrong. If any of you could give me some pointers about how to setup the mapping I'd be grateful. I hope the description of the problem is clear enough.

Leandro.
#2

[eluser]OverZealous[/eluser]
Leandro,

You might want to look at the version of DataMapper I maintain, DataMapper OverZealous Edition.

Not only does it not require 5th normal form (it allows for in-table foreign keys on $has_one objects), it has a lot of other new features and is actively maintained (DataMapper hasn't been updated since last year). The documentation is completely revamped, and I have a lot of example code included, as well as an example application.

For example, you could rewrite the code you have posted like this:
Code:
$match = new Match();
$home= new Home($homeid);
$away= new Away($awayid);
$date = new Date($dateid);

if($match->save(array($home, $away, $date))) {
    // Sucess
} else {
    // Error
}

It won't help if you have problems in your database design, but it might help get you started.
#3

[eluser]BrianDHall[/eluser]
Just throwing in my two cents, I think DMZ is the best addition to CI I've found - and the only one I use in every project now.

DMZ does support self-relationships and multiple relationships to a single model through some special advanced relationships - but I recommend avoiding such complexities and trying to design the DB in a way that avoids it. For instance as above, home and away would be related to match, and then away and home would each relate to a specific team.

At some point you could then do:

Code:
$game = new Match();
$home_team = $game->where('date', '01-10-09')->get()->home->team->name;

It requires a bit of a different approach to your DB structure, but I think you'll find it is easier to change the shape of the peg now than try to shove the octagonal peg into the polygonal whole of ORM later Smile
#4

[eluser]Unknown[/eluser]
Thanks for your help guys, I didn't try OZ Datamapper edition before thinking it wasn't really stable or actively developed. But now I see I was wrong about that. I'll be checking it out and trying to get the problem solved. If any interesting issue about it comes up, I'll share it with you.
Thanks again for your time.




Theme © iAndrew 2016 - Forum software by © MyBB