• Resolved dodmax

    (@dodmax)


    Hi,

    I’m trying to join twice on a table using a belong_to relation.
    Basing on the calendar example, in the event model I would replace:
    var $belongs_to = array('Venue');

    by:

    var $belongs_to = array(
       'Start Venue' => array('foreign_key' => 'start_venue_id'),
       'End Venue' => array('foreign_key' => 'end_venue_id')
    )

    Obviously var $includes = array('Start Venue', 'End Venue'); won’t work though.

    Anybody sorted that out?
    Thanks.

    https://www.ads-software.com/extend/plugins/wp-mvc/

Viewing 1 replies (of 1 total)
  • Thread Starter dodmax

    (@dodmax)

    Ok after a lot of try and fail I figured out part of it.
    Taking the example of a match between 2 teams.

    The Match model will contain:

    var $belongs_to = array(
        'Hometeam' => array('foreign_key' => 'home_team_id', 'class' => 'Team'),
        'Awayteam' => array('foreign_key' => 'away_team_id', 'class' => 'Team')
    );

    ‘class’ => ‘Team’ refer to the Team model name.
    The keys of the belongs_to array (‘Hometeam’, …) must have the first letter capitalized, rest lowercase, no underscore or other stuff.

    The Match admin controller will contain:

    var $default_columns = array(
        'hometeam' => array('value_method' => 'admin_column_hometeam'),
        'awayteam' => array('value_method' => 'admin_column_awayteam'),
    );
    
    public function admin_column_awayteam($object)
    {
        return $object->awayteam->name; //note that awayteam is lowercased
    }
    
    public function admin_column_hometeam($object)
    {
        return $object->hometeam->name;
    }

    Somehow something magic happens when calling $object->hometeam->name that will query the team name from the database and set it (you can see that $object->hometeam is not set before the call and is after).

    I’m still having some trouble populating the team name on the edit form since var $includes = array('Hometeam'); is causing an error…

Viewing 1 replies (of 1 total)
  • The topic ‘[Plugin: WP MVC] Models: multiple join on the same table’ is closed to new replies.