The model relationships

A true relationship is when you can tell each other anything and everything. No secrets and no lies.

I’ve had a lot of playing with ORM relationships, mostly getting it sort of right. I’ve always hating reading other peoples code trying to work out what I’m supposed to do but sometimes it’s the easiest way.

The following code fragments show my current Model implementation in regards to their ORM relationships. If you read these in conjunction with the data schema you may learn something. On the other hand you might see what I’ve done wrong. If so, don’t be shy 🙂

The big issue I had here was an error on ORM.php line 46, unable to find Model_event_competitor.

The problem was that I hadn’t capitalized the ‘model’ name in the _has_many and _belongs_to blocks.

Not so simple to spot when you’re in a hurry!

In Model_Club notice that a Club is connected to Competitors and Events through the “pivot” table BUT has a direct one-to-many relationship with the pivot table (entries)

We should be able to answer queries like:

  • How many (and what) events has this club run?
  • How many competitors (and who) have competed as members of this club
class Model_Club extends ORM
{
  protected $_primary_val = 'name';

  protected $_has_many = array(
      'competitors' => array(
           'model'   => 'Competitor',
           'through' => 'event_has_class_competitor'),
      'events' => array(
           'model'   => 'Event',
           'through' => 'event_has_class_competitor'),
      'entries' => array(
           'model'   => 'Event_Competitor',
           'foreign_key' => 'club_id'),
);

For the Bike model:

  • How many competitors have ridden this bike?
class Model_Bike extends ORM
{
  protected $_primary_val = 'name';

  protected $_has_many = array(
      'competitors' => array(
           'model'   => 'Competitor',
           'through' => 'event_has_class_competitor'));

 

class Model_Class extends ORM
{
  protected $_primary_val = 'name';

  protected $_has_many = array(
      'competitors' => array(
           'model'   => 'Competitor',
           'through' => 'event_has_class_competitor'));

 

class Model_Competitor extends ORM
{
  protected $_primary_val = 'name';

  protected $_has_many = array(
      'bikes' => array(
           'model'   => 'Bike',
           'through' => 'event_has_class_competitor'));

  protected $_belongs_to = array(
      'events' => array(
           'model'   => 'Event',
           'through' => 'event_has_class_competitor'),
      'classes' => array(
           'model'   => 'Class',
           'through' => 'event_has_class_competitor'),
      'clubs' => array(
           'model'   => 'Club',
           'through' => 'event_has_class_competitor'));

 

class Model_Event extends ORM
{
  protected $_primary_val = 'name';

  protected $_has_many = array(
      'competitors' => array(
           'model'   => 'Competitor',
           'through' => 'event_has_class_competitor'),

     'entries' => array(
           'model'   => 'Event_Competitor',
           'foreign_key' => 'event_id'));

  protected $_belongs_to = array(
      'clubs' => array(
           'model'   => 'Club',
           'foreign_key' => 'club_id'));

The dreaded NON-PIVOT PIVOT table.

// we only need this class so that we can view XTRA fields
// in event_has_class_competitor.
// .. if we were just using the table as a straight pivot table
// then all would be jake....
class Model_Event_Competitor extends ORM
{
  protected $_table_name = 'event_has_class_competitor';

  protected $_sorting = array('class_id'=>'ASC', 'total'=>'ASC');

  protected $_belongs_to = array(
      'event' => array(
           'model'   => 'Event',
           'foreign_key' => 'event_id'),
      'class' => array(
           'model'   => 'Class',
           'foreign_key' => 'class_id'),
      'bike' => array(
           'model'   => 'Bike',
           'foreign_key' => 'bike_id'),
      'competitor' => array(
           'model'   => 'Competitor',
           'foreign_key' => 'competitor_id'),
      'club' => array(
           'model'   => 'Club',
           'foreign_key' => 'club_id'));