Many-to-many associations using a join table

This strategy consist of creating a new table which responsibility is to reference each side of the relationship. if you call the tableUpdate method, the table is automatically created and its name respond to the following convention: ClassA_ClassB where ClassA < ClassB. The 2 fields created in this table respond to the following convention: class_a_id and class_b_id where id is the primary key of ClassA and ClassB.

Storing relationships with a join table is the fastest strategy. It also present the advantage of having the ability to values specific to the relationship (for exemple, we could image the date when the relationship is created). However, storing values specific to a relationship is not yet handled.

You will be using this strategy if the two sides of the relationship define an has_many field (and if none of them is marked as transient).

Setting up the "has_many" property for the "join table" strategy

The following keys may be used:

Quick exemple

An has_many relationship with a join table


class Groups extends PorteRecord{
  $meta_fields = array(
    'users' => array(
      'has_many' => true
    )
  )
}

class User extends PorteRecord{
  $meta_fields = array(
    'groups' => array(
      'has_many' => true
    )
  )
}
      

Calling "table->drop" method

When requesting to drop a table, the join table will be dropped as well.

Open Source Object Relational Mapping in PHP

Download Porte 0.2.1