Title: [Plugin: WP MVC] Models: multiple join on the same table
Last modified: August 20, 2016

---

# [Plugin: WP MVC] Models: multiple join on the same table

 *  Resolved [dodmax](https://wordpress.org/support/users/dodmax/)
 * (@dodmax)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/plugin-wp-mvc-models-multiple-join-on-the-same-table/)
 * 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.
 * [http://wordpress.org/extend/plugins/wp-mvc/](http://wordpress.org/extend/plugins/wp-mvc/)

Viewing 1 replies (of 1 total)

 *  Thread Starter [dodmax](https://wordpress.org/support/users/dodmax/)
 * (@dodmax)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/plugin-wp-mvc-models-multiple-join-on-the-same-table/#post-2864522)
 * 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.

 * ![](https://s.w.org/plugins/geopattern-icon/wp-mvc_ffffff.svg)
 * [WP MVC](https://wordpress.org/plugins/wp-mvc/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/wp-mvc/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/wp-mvc/)
 * [Active Topics](https://wordpress.org/support/plugin/wp-mvc/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wp-mvc/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wp-mvc/reviews/)

 * 1 reply
 * 1 participant
 * Last reply from: [dodmax](https://wordpress.org/support/users/dodmax/)
 * Last activity: [13 years, 11 months ago](https://wordpress.org/support/topic/plugin-wp-mvc-models-multiple-join-on-the-same-table/#post-2864522)
 * Status: resolved