Created
June 4, 2014 08:42
-
-
Save ineersa/2b52597dd5a60d0af236 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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