-
Notifications
You must be signed in to change notification settings - Fork 5
model behavior weighable
Garp_Model_Db_Weighable ensures a custom sort order for records.
It's used when the records of a model are fetched associatively (e.g. when it was bound to another model via bindModel()) and sorted in no particular order other than "personal preference". In order to use it, configure the following:
Your database table should have an INT type column to store the order in. Pass this column to the behavior and register the behavior, like this:
<?php
$this->registerObserver(new Garp_Model_Behavior_Weighable(
array(
'Model_User' => array(
'foreignKeyColumn' => 'user_id',
'weightColumn' => 'weight'
)
)
);
?>
Note that a foreign key is required since this behavior only acts upon related data.
This behavior does the following stuff for you:
When you fetch related data, the associatively fetched records are automatically sorted by the weight column:
<?php
// Weighable behavior is registered on Model_Comment
$userModel = new Model_User();
$userModel->bindModel('Model_Comment');
$users = $userModel->fetchAll();
// the associated comments will be sorted by the custom weight column
?>
When you relate records to one another, the configured weight column is maintained so that administrators can easily drag and drop a custom order in the CMS interface.
In a hasMany or hasOne relationship, the behavior must be registered on the model that contains the weight column.
In a hasAndBelongsToMany relationship, the behavior must be registered on the intersection model (for instance: Model_TagUser).
Take note that when managing a hasAndBelongsToMany relationship, it makes the most sense to maintain the order from one side of the relationship only. Consider the following:
User David has the following tags, in this order: nerd, moustache, male.
In this case, the weight column for the David - moustache relationship will contain the integer 2.
But what if that tag is linked not only to David, but to a couple of other people as well? And an admin wants to sort these people in a custom order as well? Let's say the following:
Tag moustache is related to the following users, in this order: David, Harmen, Jelmer.
This clashes with the above mentioned order, because now the weight column associated with David and the moustache tag should contain the integer 3 (note that the weight column is sorted descending).
This might be a problem, but it can easily be solved by using two separate weight columns, for instance user_tag_weight and tag_user_weight.
This can be configured like so:
<?php
$this->registerObserver(new Garp_Model_Behavior_Weighable(
array(
'Model_Tag' => array(
'foreignKeyColumn' => 'tag_id',
'weightColumn' => 'tag_user_weight'
),
'Model_User' => array(
'foreignKeyColumn' => 'user_id',
'weightColumn' => 'user_tag_weight'
),
)
));
?>
Of course, this might provide some confusion for whoever's looking through the database, but at least it makes the above scenario a possibility.
If managing the relationship order from one side only is no problem, just define the one foreign key and associated weight column.