30 throw new CHttpException(400, Yii::t(
'yii',
'Your request is invalid.'));
32 $sortableAttribute = Yii::app()->request->getQuery(
'sortableAttribute');
36 if (!$model->hasAttribute($sortableAttribute)) {
37 throw new CHttpException(400, Yii::t(
39 '{attribute} "{value}" is invalid.',
40 array(
'{attribute}' =>
'sortableAttribute',
'{value}' => $sortableAttribute)
44 $sortOrderData = $_POST[
'sortOrder'];
46 $this->
update($model, $sortableAttribute, $sortOrderData);
51 return Yii::app()->request->isPostRequest
52 && Yii::app()->request->isAjaxRequest
53 && isset($_POST[
'sortOrder']);
63 private function update($model, $sortableAttribute, $sortOrderData)
65 $pk = $model->tableSchema->primaryKey;
68 $string_ids = array_keys($sortOrderData);
71 foreach ($string_ids as $string_id)
72 $array_ids[] = explode(
',', $string_id);
74 foreach ($array_ids as $array_id)
75 $pk_array[] = array_combine($pk, $array_id);
77 $pk_array = array_keys($sortOrderData);
80 $models = $model->model()->findAllByPk($pk_array);
81 $transaction = Yii::app()->db->beginTransaction();
83 foreach ($models as $model) {
84 $_key = is_array($pk) ? implode(
',', array_values($model->primaryKey)) : $model->primaryKey;
85 $model->{$sortableAttribute} = $sortOrderData[$_key];
88 $transaction->commit();
91 $transaction->rollback();