Fork me on GitHub

Associations

View alone

Belongs To

To define one or more belongs to associations, set the belongs_to property.

<?php
class Event extends MvcModel {

  var $belongs_to = array('Venue');

}
?>

In this example, the events table should have a venue_id column, which will be used as the foreign key.

After this association has been defined, you can now easily include associated data in finds {add more}.

A custom foreign key can also be defined:

<?php
class Event extends MvcModel {

  var $belongs_to = array(
    'Venue' => array(
      'foreign_key' => 'my_venue_id'
    )
  );

}
?>
View alone

Has Many

To define one or more has many associations, set the has_many property.

<?php
class Venue extends MvcModel {

  var $has_many = array('Event');

}
?>
View alone

Has and Belongs To Many

To define one or more has and belongs to many associations, set the has_and_belongs_to_many property.

WP MVC will attempt a guess at the join table, but you may want to specify it (use {prefix} in the table name as a token that will be replaced by $wpdb->prefix). You can also specify what fields are retrieved from the table.

<?php
class Event extends MvcModel {
  var $has_and_belongs_to_many = array(
    'Speaker' => array(
      'join_table' => '{prefix}events_speakers',
      'fields' => array('id', 'first_name', 'last_name')
    )
  );
}
?>
View alone

Dependent Associations

If an association is set as being dependent, when the parent object is deleted, all of the associated objects that belong to it will be deleted, too.

In the following example, if a List object is deleted, all of its ListItems will be deleted, too.

<?php
class List extends MvcModel {

  var $has_many = array(
    'ListItem' => array(
      'dependent' => true
    )
  );

}
?>