Positioning object one relative to the others
A record is marked as being positionable if its model define a property "position" in its table definition. A new property "position" is made available to the record with a couple of usefull methods.
Available methods
-
Method "after"
Place the current record after the provided record. Changes will take effect after calling the "save" method. -
Method "before"
Place the current record before the provided record. Changes will take effect after calling the "save" method. -
Method "first"
Place the current record af the first record. Its position value will be set to 1. Changes will take effect after calling the "save" method. -
Method "last"
Place the current record af the last record. Its position value should be equal to the value returned by the "count" method. Changes will take effect after calling the "save" method. -
Method "previous"
Place the current record one or more step before. Changes will take effect after calling the "save" method. -
Method "next"
Place the current record one or more step forward. Changes will take effect after calling the "save" method. -
Method "max"
Return the greatest position value. It is equals to the value returned by the "count" method of the record. -
Method "current"
Return the current position value. -
Method "get{PositionField}"
Return the same value as the method "current". By default, Porte use a property "position" so the available method is "getPosition".
Exemple
Command: "php samples/position/simple-exemple.php"
class User extends PorteRecord{
public $meta_table = array(
'position' => true
);
}
$user1 = new User();
$user2 = new User();
$user3 = new User();
$user1->table->update(array('drop'=>true));
echo 'Created fields in table: ';
echo implode(', ',array_keys($user1->table->fields()))."\r\n";
$user1->save();
$user2->save();
$user3->save();
echo '1st user position: '.$user1->getPosition()."\r\n";
echo '2nd user position: '.$user2->getPosition()."\r\n";
echo '3rd user position: '.$user3->getPosition()."\r\n";
echo 'We move 3rd user at first position'."\r\n";
$user3->position->first();
$user3->save();
echo '1st user position: '.$user1->getPosition()."\r\n";
echo '2nd user position: '.$user2->getPosition()."\r\n";
echo '3rd user position: '.$user3->getPosition()."\r\n";
echo 'We move back 3rd user at last position after 2nd user'."\r\n";
$user3->position->after($user2);
$user3->save();
echo '1st user position: '.$user1->getPosition()."\r\n";
echo '2nd user position: '.$user2->getPosition()."\r\n";
echo '3rd user position: '.$user3->getPosition()."\r\n";
// Created fields in table: id, position
// 1st user position: 1
// 2nd user position: 2
// 3rd user position: 3
// We move 3rd user at first position
// 1st user position: 2
// 2nd user position: 3
// 3rd user position: 1
// We move back 3rd user at last position after 2nd user
// 1st user position: 1
// 2nd user position: 2
// 3rd user position: 3
Note: Change will take effect once the "save" method is called, not straight after calling one the above methods.
Configuration
A record is marked as positionable if its meta_table property contains a key "position". You can set options if the value of the key "position" is an array.
Command: "php samples/position/configuration.php"
class User extends PorteRecord{
public $meta_table = array(
'position' => true
);
}
$user = new User();
$user->table->update(array('drop'=>true));
$user->save();
echo 'User urder value is: '.$user->getPosition()."\r\n";
class File extends PorteRecord{
public $meta_table = array(
'position' => array(
'field' => 'position_field'
)
);
}
$file = new File();
$file->table->update(array('drop'=>true));
$file->save();
echo 'File position value is: '.$file->getPositionField()."\r\n";
// User urder value is: 1
// File position value is: 1
Understanding the internal
The "position" services use the "event" system to plug required database organisation when the current record is saved.
If calling the "reset" or "reload" methods, the "position" action will be canceled.