Akelos Framework v1 forum archive. This forum is no longer maintained. To report bugs please visit https://github.com/akelos/akelos/issues
    • CommentAuthorreeceer
     
    I'm learning the Akelos framework as a newbie. I really like the framework but I'm struggling with one part of my application.

    My application has a schedule with weeks - each week has some number of team matchups (1 vs. 2, 3 vs. 4, etc.) At first I went down the road of a habtm association between teams and team_matchups in a team_matchups_teams table. But, after further consideration I'd like to be able to have a Home team id and Away team id on the team_matchups table. I thought I could put a has_one relationship on team_matchup for a home_team and away_team both represented by the teams table. I can't figure out how to do this.

    Basically, I'd like to be able to have the following on the team_matchups records:

    Week: 1, Home_Team: 1, Away_Team: 2
    Week: 1, Home_Team: 3, Away_Team: 4
    .....
    Week 6, Home_Team: 2, Away_Team: 4
    Week 6, Home_Team 6, Away_Team: 1

    Is there a way in the models has_one declaration or in the var $models declaration in the controller to specify the table name?

    Basically it is simple inheritance on the object but both home_team_id and away_team_id point to the team_id column in the teams table. Can this be done?

    //SELECT HOME AND AWAY TEAM OF A GIVEN TEAM_MATCHUP - $ID
    SELECT TM.WEEK_ID, HOME.ID, HOME.NAME, AWAY.ID, AWAY.NAME
    FROM
    TEAMS HOME,
    TEAMS AWAY,
    TEAM_MATCHUPS TM
    WHERE HOME.ID = TM.HOME_TEAM_ID AND
    AWAY.ID = TM.AWAY_TEAM_ID AND
    TM.ID = 1

    OUTPUT:
    WEEK: 1 HOME TEAM: 1 NAME: TIGERS VS. AWAY TEAM: 2 NAME: BEARS
    • CommentAuthorsuthern
     
    I'd suggest this setup:
    models: team, match, location.
    columns/fields for 'team': name, contact_person, phone, whatever.
    columns/fields for 'match': home_id, visitor_id, location_id, home_score, visitor_score, played_at (*_at means date & time , not physical location of field).
    columns/fields for 'location': field_name, city, state, zip, contact_person, contact_phone, whatever.

    I'm not sure how to make akelos accept that there are two keys to the SAME TABLE inside each record of TEAM_MATCH (Kaste? Bermi?), but you can do anything using custom SQL. For instance in YOUR example, I think you are looking for something like this:

    SELECT tm.week_id, home.id, home.name, away.id, away.name
    FROM team_matchups AS tm
    LEFT OUTER JOIN teams AS home ON home.id = tm.home_team_id
    LEFT OUTER JOIN teams AS away ON away.id = tm.away_team_id
    WHERE tm.id = 1

    As an aside, I like to do all my SQL COMMANDS in CAPS, but all my tablenames and such (inside the sql commands) in lower case. This makes it easier to recognize what is what IMHO.
    • CommentAuthorKaste
     

    Ok, I don't get through the complete example, but using options when declaring the association should work.

    Given a Post belongs_to an Author. But the Author is actually a Person.

    class Post...
    var belongs_to = array('author'=>array('class_name'=>'Person','primary_key_name'=>'written_by'));
    
    class Person...
    var has_many = array('Post'=>array('foreign_key'=>'written_by'));
    

    so a Match could belong_to a HomeTeam with

     class_name: Team
     primary_key_name: HOME_TEAM_ID
    

    hope it helps

    • CommentAuthorreeceer
     
    kaste - you are almost there but rather than just one person - suppose you have co-authors (only and always 2) on each post. So, instead of author you have author AND co-author - both of which are actually saved as a Person record. So a Post would have an author_id (actually Person.id) and a co_author_id (also actually Person.id.) So, I kind of get specifying the class name if it is different than the default conventions but in my example - home_team_id and away_team_id are foreign keys not primary keys. Just as in this post - author_id and co_author_id are foreign keys that both go to Person.id (the primary key on author and co-author - since they use the same table.)

    suthern - appreciate the input and yes I normally follow the same convention for writing SQL - why I did all CAPS for my post probably had to do with me posted it at like 2 AM or something. Anyway, I realize custom SQL could work but I was hoping to stick within the framework in most/if not all cases. This seemed like a fairly common pattern so I figured there is something there along the lines of options - now I just need help with the right ones.
    • CommentAuthorKaste
     

    is this a semantic thing?

    class Match...
    var $belongs_to = array(
        'home_team' => array('class_name'=>'Team','primary_key_name'=>'home_team_id'),
        'away_team' => array('class_name'=>'Team','primary_key_name'=>'away_team_id')
    );    
    

    primary_key_name refers to a local_column. It is primary to the association not to a table. I.e. it is not the primary_key of neither the Match_Model nor the Team-Model, but it points to the primary_key of the Team-Model.

    Now:

    $Match = $this->Match->find('first',array('include'=>array('home_team','away_team')));
    
    $Match->home_team->name
    $Match->away_team->name
    //given the teams-table has a column 'name'
    
    • CommentAuthorreeceer
     
    I believe that was the part I was missing. Thanks for the semantic explanation. I'll give it a try.

    BTW, love the framework and love the responsiveness of this forum.
    • CommentAuthorreeceer
     
    well - It's working - kind of. I get the following error when I try to view the attributes on the home_team or away_team objects in the listing view.

    Undefined property: AkAssociatedActiveRecord::$name in C:\devtools\golfleague\app\views\team_matchup\compiled\listing.tpl.php on line 43

    In the controller I'm including the home_team and away_team in the find options.

    Here is the listing action:
    $this->team_matchups =& $this->TeamMatchup->find('all',
    array('include'=> array('home_team','away_team','week')),
    $this->pagination_helper->getFindOptions($this->TeamMatchup));

    and in the listing.tpl
    {loop team_matchups}
    <td class="field">"Home Team: ".<?php echo $TeamMatchup->home_team->name ?>." vs. Away Team: ".<?php $TeamMatchup->away_team->name ?></td>
    {end}

    Here is the show action:
    $this->team_matchup = $this->TeamMatchup->find(@$this->params['id'], array('include'=> array('home_team','away_team','week')));

    and in the show.tpl
    <label>Home Team: <?php echo $TeamMatchup->home_team->name ?> vs. Away Team: <?php echo $TeamMatchup->away_team->name ?></label><br />

    I'm accessing both the same way in the views - $TeamMatchup->home_team->name OR $TeamMatchup->away_team->name but in the show action it works in the listing action it doesn't.

    What am I doing wrong?
    • CommentAuthorKaste
     

    #

    $this->TeamMatchup->find('all', 
        array('include'=> array('home_team','away_team','week')),
        $this->pagination_helper->getFindOptions($this->TeamMatchup));
    

    The finder only takes one options-array, as its last argument.

    Try

    $options = array_merge(paginator_options,array('include'...));
    

    something like this.