是否可以在PHP ActiveRecord中表示第五种常规形式?

时间:2012-05-29 16:19:48

标签: php activerecord database-normalization

是否可以在PHP ActiveRecord中表示第五个普通表单?

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

1 个答案:

答案 0 :(得分:3)

第五个法线可以用“$ 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. 

在此示例中,订单通过转到与用户相关联 其支付协会。

 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

我在这里找到了答案:

您可以为联合表创建模型,然后通过联接表关联手机表!

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

还发现了这个: http://svn.phpontrax.com/wiki/ActiveRecordTableAssociations