public static function shift_field(Resource_Model $resource, $direction = 'up') { // Make sure we have a valid resource (and only one!) if(count($resource) === 1) { // What direction are we moving it? list($direction, $sort) = (strtolower($direction) === 'up') ? array('<','DESC') : array('>','ASC'); // Get resource information $resource->current(); // Get the field we are going to swap with $field_for_swap = Resource_Model::factory() ->where('sort_order '.$direction.$field->sort_order) ->where('resource_id', $field->resource_id) ->orderby('sort_order', $sort) ->limit(1) ->get() ->current(); // If we found a field to swap with, then let's update them if (count($field_for_swap === 1)) { // Swap the values list($resource->sort_order, $field_for_swap->sort_order) = array($field_for_swap->sort_order, $resource->sort_order); // Save the changes! $resource->save(); $field_for_swap->save(); } } }