Search code examples
phpactiverecorddatabase-normalization

Is it possible to represent the fifth normal form in PHP ActiveRecord?


Is it possible to represent the fifth normal form in PHP ActiveRecord?

i.e.

Person <=> (person_id) person_phone (phone_id) <=> Phone

Solution

  • The fifth normal can be represented with "$has_many through"

    http://www.phpactiverecord.org/projects/main/wiki/Associations

    has_many through (many to many)
    
    This is a convenient way to configure a many-to-many association. 
    

    In this example an order is associated with users by going the its payments association.

     1 class Order extends ActiveRecord\Model {
     2   static $has_many = array(
     3     array('payments'),
     4     array('users', 'through' => 'payments')
     5   );
     6 }
     7 
     8 class Payment extends ActiveRecord\Model {
     9   static $belongs_to = array(
    10     array('user'),
    11     array('order')
    12   );
    13 }
    14 
    15 class User extends ActiveRecord\Model {
    16   static $has_many = array(
    17     array('payments')
    18   );
    19 }
    20 
    21 $order = Order::first();
    22 # direct access to users
    23 print_r($order->users); # will print an array of User object
    

    I found the answer here:

    You could create a model for the joint table, and then associate the phone table through the join table!

    http://www.phpactiverecord.org/boards/4/topics/226

    Also found this: http://svn.phpontrax.com/wiki/ActiveRecordTableAssociations