Many-to-many associations using a list of foreign keys

This strategy consist in storing in one of the database field a list of primary keys. In our groups/users exemple, we could imaging a field name users in the groups table with a value of '34,58', which means this group has users with primary keys equal to 34 and 58. If another group has a value '34,93', then the user with primary key equals to 34 is member of the 2 groups.

Storing relationships as a list of primary keys present the advantage of being easier to administer. However, keep in mind this is not the most efficient approach.

You will be using this strategy if only one side of the relationship declare an has_many association (eg groups declare to have many users, but users does not declare to have many groups) or if the 2 side declare the has_many relationship but one of the field is declared as transient.

Setting up the "has_many" property for the "list of foreign keys" strategy

Quick exemple

An has_many relationship defined on one side only


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

class User extends PorteRecord{
  $meta_fields = array(
  )
}
    

An has_many relationship defined on two sides with a transient field


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

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

Open Source Object Relational Mapping in PHP

Download Porte 0.2.1