Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Data Model Questions /

[SOLVED] One $has_many to TWO $has_one relationships. Awkward problem.


Go to End
Reply


12 Posts   1837 Views

Avatar
Sticks

Community Member, 31 Posts

19 October 2011 at 6:04pm

I have a problem with a $has_many relationship where not all the associated objects are being returned.

I'm attempting to create a relatively simple sporting league system on a SilverStripe site. The two classes I'm having trouble with are for Match and Team. Each match has two teams, a home team and an away team, setup like so:

Match.php

..
static $has_one = array (
'HomeTeam' => 'Team',
'AwayTeam' => 'Team'
);
..

Team.php

..
static $has_many = array (
'Matches' => 'Match'
);
..

Now the teams are actually created in the League page class, and there are dropdowns in the Match DOM to select the team for HomeTeam and another for AwayTeam. The Match class is created in a 'Round' page class.

My problem is it seems only the matches where the current team is set as the away team are being added to the has_many relationship. In other words calling $this->Matches in the Team class returns only matches where the team was set as the AwayTeam, and none of the matches where it set as the HomeTeam.

Match.php

$this->HomeTeam; //returns the correct Team object fine
$this->AwayTeam; //returns the correct Team object fine

Team.php

$this->Matches(); //only returns matches where the team is set as the away team, not the home team.

Since every team in the league can be a HomeTeam and an AwayTeam depending on the match, it doesn't seem feasible to create HomeTeam and AwayTeam sub-classes. Having the two $has_one relationships in the Match where a team could be either or both of those is having me scratching my head.

Should this be a $many_many relationship? I'm still learning SilverStripe and OOP in general and the $many_many relationship looks like a tricky one.

Thanks for your time.

Avatar
Willr

Forum Moderator, 5513 Posts

19 October 2011 at 7:21pm

I believe you can name each of those relationships to make it to work. I can't remember (or fine) the exact syntax for this but I think it's something like..

..
'HomeTeam' => 'Team.Home',
'AwayTeam' => 'Team.Away'
..

Avatar
swaiba

Forum Moderator, 1799 Posts

19 October 2011 at 10:59pm

I've seen a post that started out like that and then they ended with some "oh it doesn't work" thing - I think.
Maybe two has_one's work, but I am fairly sure that two many_many's between the same object don't at least I have hacked my way around this because I couldn't get ModelAdmin to scaffold them. Hopefully this will all be fine in SS3...

Avatar
martimiz

Forum Moderator, 1106 Posts

20 October 2011 at 2:54am

Edited: 20/10/2011 2:56am

What Willr is saying is true - see: http://doc.silverstripe.org/sapphire/en/topics/datamodel#has-many

Each has_one needs to point to its own has_many vv. So if Match does this:

static $has_one = array (
   'HomeTeam' => 'Team',
   'AwayTeam' => 'Team'
);

Then Team should do this:

static $has_many = array (
   'MatchesHome' => 'Match.HomeTeam',
   'MatchesAway' => 'Match.AwayTeam'
);

Now in your template you can:

<% control MatchesHome %> ... <% end_control %>

But if you just want all matches, you can still do something like this:

function getMatches() {
   return DataObject::get('Match', "HomeTeamID = '$this->ID' OR AwayTeamID = '$this->ID'");
}

Avatar
Willr

Forum Moderator, 5513 Posts

20 October 2011 at 5:21pm

Aha thanks martimiz! I knew it was documented *somewhere* :)

Avatar
swaiba

Forum Moderator, 1799 Posts

20 October 2011 at 7:32pm

Any word on two many_many relationship between the same two objects?
As I said that is the one that I am fairly sure doesn't work currently... am I wrong here or is it something that might be improved?

Avatar
ajshort

Community Member, 244 Posts

20 October 2011 at 7:33pm

The same syntax is used for many many relationships.

Avatar
simon_w

Forum Moderator, 474 Posts

20 October 2011 at 9:06pm

swaiba is correct. The dot notation when specifying a many-many relation does not work in 2.4 (I have no idea if it has been fixed in 3.0). When there is only two many-manys, you can set one up from class A to class B and the other from class B to class A, with belongs_many_many defined as needed.

I have needed to have three many_many relations between the same two classes, so have a working (though not very well tested) patch for allowing the dot notation when defining belongs_/many_many relations, which you can grab from [url]http://assets.simon.geek.nz/text/k[/url]

Go to Top