Skip to content

Instantly share code, notes, and snippets.

@ineersa
Created June 4, 2014 08:42
Show Gist options
  • Save ineersa/2b52597dd5a60d0af236 to your computer and use it in GitHub Desktop.
Save ineersa/2b52597dd5a60d0af236 to your computer and use it in GitHub Desktop.
<?php
public function setupOrder($old,$new)
{
$max=Yii::app()->db->createCommand("SELECT MAX(sort) FROM {$this->tableName()} WHERE app_id={$this->app_id};")->queryScalar();
$this->sort=$new;
if ($this->validate()){
if ($new>$max) $new=$max;
} else {
if ($max>=20) $new=20;
else $new=$max;
}
if ($old>$new){
Yii::app()->db->createCommand("UPDATE {$this->tableName()} SET sort=sort+1
WHERE sort>=:new AND sort<:old AND app_id=:app;")
->bindParam(":new",$new,PDO::PARAM_INT)
->bindParam(":app",$this->app_id,PDO::PARAM_INT)
->bindParam(":old",$old,PDO::PARAM_INT)
->execute();
Yii::app()->db->createCommand("UPDATE {$this->tableName()} SET sort=:new WHERE id=:id;")
->bindParam(":new",$new,PDO::PARAM_INT)
->bindParam(":id",$this->id,PDO::PARAM_INT)
->execute();
} elseif ($old<$new){
Yii::app()->db->createCommand("UPDATE {$this->tableName()} SET sort=sort-1
WHERE sort<=:new AND sort>:old AND app_id=:app;")
->bindParam(":new",$new,PDO::PARAM_INT)
->bindParam(":app",$this->app_id,PDO::PARAM_INT)
->bindParam(":old",$old,PDO::PARAM_INT)
->execute();
Yii::app()->db->createCommand("UPDATE {$this->tableName()} SET sort=:new WHERE id=:id;")
->bindParam(":new",$new,PDO::PARAM_INT)
->bindParam(":id",$this->id,PDO::PARAM_INT)
->execute();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment