fuelPHP Related Models

Just as a point of reference, nothing here that isn’t in the fuelPHP documentation.

Using fuelPHP Related Models.

All relationships are declared in the Model of course.

Belongs to

A quote will belong to a client, and also a user.
[code]
protected static $_belongs_to = array(
‘user’ => array(
‘key_from’ => ‘user_id’,
‘model_to’ => ‘Model_User’,
‘key_to’ => ‘id’,
‘cascade_save’ => true,
‘cascade_delete’ => false,
),
‘client’ => array(
‘key_from’ => ‘client_id’,
‘model_to’ => ‘Model_Client’,
‘key_to’ => ‘id’,
‘cascade_save’ => true,
‘cascade_delete’ => false,
)
);
[/code]

Has Many

A quote also has_many quote items:
[code]
protected static $_has_many = array(
‘items’ => array(
‘key_from’ => ‘id’,
‘model_to’ => ‘Model_Quote_Item’,
‘key_to’ => ‘quote_id’,
‘cascade_save’ => true,
‘cascade_delete’ => true,
)
);
[/code]

Has One

A quote has oneHas Many

A User has many Quotes:
[code]
protected static $_has_many = array(
‘quotes’ => array(
‘key_from’ => ‘id’,
‘model_to’ => ‘Model_Quote’,
‘key_to’ => ‘client_id’,
‘cascade_save’ => true,
‘cascade_delete’ => false,
)
);
[/code]

Two-way relationship

If Quote has many QuoteItems, a QuoteItem will belong to a Quote, so in the QuoteItem Model we say:

[code]
protected static $_belongs_to = array(
‘quote’ => array(
‘key_from’ => ‘quote_id’,
‘model_to’ => ‘Model_Quote’,
‘key_to’ => ‘id’,
‘cascade_save’ => true,
‘cascade_delete’ => false,
)
);
[/code]

QuoteItems also have one rate, and type:

[code]
protected static $_has_one = array(
‘type’ => array(
‘key_from’ => ‘type_id’,
‘model_to’ => ‘Model_QuoteItemType’,
‘key_to’ => ‘id’,
‘cascade_save’ => false,
‘cascade_delete’ => false,
),
‘rate’ => array(
‘key_from’ => ‘rate_id’,
‘model_to’ => ‘Model_QuoteItemRate’,
‘key_to’ => ‘id’,
‘cascade_save’ => false,
‘cascade_delete’ => false,
)
);
[/code]




No Comments


You can leave the first : )



Leave a Reply

Your email address will not be published. Required fields are marked *