How do I get the owner record in a one to many in php active record

118 Views Asked by At

A customer has many orders. (one to many relationship) What I want to do is this :

$order = Order::find_by_id(3);
echo $order->customer;

How do I set up the models to be able to do this?

1

There are 1 best solutions below

0
On

The correct way to do a one to many relationship is (e.g. customer 1 <-----> * orders)

class Customer extends ActiveRecord\Model
{
  static $has_many = array(
   array('orders')
  );    
}

class Order extends ActiveRecord\Model
{
  static $belongs_to = array(
    array('customer')
  );     
}

This was not clear from the php active record documentation.