Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » [SOLVED] One $has_many to TWO $has_one relationships. Awkward problem.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1 2
Go to End
Author Topic: 1753 Views
  • Sticks
    Avatar
    Community Member
    29 Posts

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

    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.

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: [SOLVED] One $has_many to TWO $has_one relationships. Awkward problem. Link to this post

    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'
    ..

  • swaiba
    Avatar
    Forum Moderator
    1792 Posts

    Re: [SOLVED] One $has_many to TWO $has_one relationships. Awkward problem. Link to this post

    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...

  • martimiz
    Avatar
    Forum Moderator
    1086 Posts

    Re: [SOLVED] One $has_many to TWO $has_one relationships. Awkward problem. Link to this post

    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'");
    }

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: [SOLVED] One $has_many to TWO $has_one relationships. Awkward problem. Link to this post

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

  • swaiba
    Avatar
    Forum Moderator
    1792 Posts

    Re: [SOLVED] One $has_many to TWO $has_one relationships. Awkward problem. Link to this post

    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?

  • ajshort
    Avatar
    Community Member
    244 Posts

    Re: [SOLVED] One $has_many to TWO $has_one relationships. Awkward problem. Link to this post

    The same syntax is used for many many relationships.

  • simon_w
    Avatar
    Forum Moderator
    473 Posts

    Re: [SOLVED] One $has_many to TWO $has_one relationships. Awkward problem. Link to this post

    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 http://assets.simon.geek.nz/text/k

    1753 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.