2012-01-15 71 views

回答

6

我做了一個函數來獲得你要找的那些id。我建議你宣佈它的模型:

public static function getNextOrPrevId($currentId, $nextOrPrev) 
{ 
    $records=NULL; 
    if($nextOrPrev == "prev") 
     $order="id DESC"; 
    if($nextOrPrev == "next") 
     $order="id ASC"; 

    $records=YourModel::model()->findAll(
     array('select'=>'id', 'order'=>$order) 
     ); 

    foreach($records as $i=>$r) 
     if($r->id == $currentId) 
      return isset($records[$i+1]->id) ? $records[$i+1]->id : NULL; 

    return NULL; 
} 

因此,要使用它,你需要做的做的是這樣的:

YourModel::getNextOrPrevId($id /*(current id)*/, "prev" /*(or "next")*/); 

它會返回一個或下一個記錄的對應的ID。

我沒有測試它,所以試試看,如果出現問題,請讓我知道。

+0

" class="dBlock left">Next Abudayah 2012-01-16 08:04:01

+0

我寫PhotoController該功能 – Abudayah 2012-01-16 08:06:07

+0

致命錯誤:既然你在控制器中聲明該函數調用未定義的方法照片:: getPrevOrNextId() – Abudayah 2012-01-16 08:12:12

2

創建一個私有變量,用於將信息傳遞給其他函數。

在型號:

class Model1 ..... 
{ 
    ... 
    private _prevId = null; 
    private _nextId = null; 
    ... 

    public function afterFind() //this function will be called after your every find call 
    { 
    //find/calculate/set $this->_prevId; 
    //find/calculate/set $this->_nextId; 
    } 

    public function getPrevId() { 
     return $this->prevId; 
    } 

    public function getNextId() { 
     return $this->nextId; 
    } 

} 

檢查在ViewDetal鏈接生成和代碼修改在_view文件中的上一個/淨鏈路陣列( 'ID' 中使用

$model(or $data)->prevId/nextId 

= >#)部分。

6

我說按照我的模型功能Yii2:

public function getNext() { 
    $next = $this->find()->where(['>', 'id', $this->id])->one(); 
    return $next; 
} 

public function getPrev() { 
    $prev = $this->find()->where(['<', 'id', $this->id])->orderBy('id desc')->one(); 
    return $prev; 
} 
+1

迄今爲止最好的答案。大多數人使用 - > all(),這是浪費資源IMO。 – vanarie 2018-02-04 07:50:20

1

以原來的答案,用少許清理適應它Yii2:

/** 
* [nextOrPrev description] 
* @source http://stackoverflow.com/questions/8872101/get-next-previous-id-record-in-database-on-yii 
* @param integer $currentId [description] 
* @param string $nextOrPrev [description] 
* @return integer   [description] 
*/ 
public static function nextOrPrev($currentId, $nextOrPrev = 'next') 
{ 
    $order = ($nextOrPrev == 'next') ? 'id ASC' : 'id DESC'; 
    $records = \namespace\path\Model::find()->orderBy($order)->all(); 

    foreach ($records as $i => $r) { 

     if ($r->id == $currentId) { 
      return ($records[$i+1]->id ? $records[$i+1]->id : NULL); 
     } 

    } 

    return false; 
} 
0

以前的解決方案是有問題的,當你獲取第一條或最後一條記錄,並且他們正在對數據庫進行多次調用。這裏是一個查詢操作我工作的解決方案,處理結束表和結束表禁用按鈕:

在模型:

public static function NextOrPrev($currentId) 
{ 
    $records = <Table>::find()->orderBy('id DESC')->all(); 

    foreach ($records as $i => $record) { 
     if ($record->id == $currentId) { 
      $next = isset($records[$i - 1]->id)?$records[$i - 1]->id:null; 
      $prev = isset($records[$i + 1]->id)?$records[$i + 1]->id:null; 
      break; 
     } 
    } 
    return ['next'=>$next, 'prev'=>$prev]; 
} 

內的控制器:

 public function actionView($id) 
{ 
    $index = <modelName>::nextOrPrev($id); 
    $nextID = $index['next']; 
    $disableNext = ($nextID===null)?'disabled':null; 
    $prevID = $index['prev']; 
    $disablePrev = ($prevID===null)?'disabled':null; 

    // usual detail-view model 
    $model = $this->findModel($id); 

    return $this->render('view', [ 
     'model' => $model, 
     'nextID'=>$nextID, 
     'prevID'=>$prevID, 
     'disableNext'=>$disableNext, 
     'disablePrev'=>$disablePrev, 
    ]); 
} 

在視圖裏:

<?= Html::a('Next', ['view', 'id' => $nextID], ['class' => 'btn btn-primary r-align btn-sm '.$disableNext]) ?> 
<?= Html::a('Prev', ['view', 'id' => $prevID], ['class' => 'btn btn-primary r-align btn-sm '.$disablePrev]) ?> 
1

我的實現是基於SearchModel。

控制器:

public function actionView($id) 
{ 
    // ... some code before 

    // Get prev and next orders 
    // Setup search model 
    $searchModel = new OrderSearch(); 
    $orderSearch = \yii\helpers\Json::decode(Yii::$app->getRequest()->getCookies()->getValue('s-' . Yii::$app->user->identity->id)); 
    $params = []; 
    if (!empty($orderSearch)){ 
     $params['OrderSearch'] = $orderSearch; 
    } 
    $dataProvider = $searchModel->search($params); 
    $sort = $dataProvider->getSort(); 
    $sort->defaultOrder = ['created' => SORT_DESC]; 
    $dataProvider->setSort($sort); 

    // Get page number by searching current ID key in models 
    $pageNum = array_search($id, array_column($dataProvider->getModels(), 'id')); 
    $count = $dataProvider->getCount(); 
    $dataProvider->pagination->pageSize = 1; 

    $orderPrev = $orderNext = null; 
    if ($pageNum > 0) { 
     $dataProvider->pagination->setPage($pageNum - 1); 
     $dataProvider->refresh(); 
     $orderPrev = $dataProvider->getModels()[0]; 
    } 
    if ($pageNum < $count) { 
     $dataProvider->pagination->setPage($pageNum + 1); 
     $dataProvider->refresh(); 
     $orderNext = $dataProvider->getModels()[0]; 
    } 
    // ... some code after 
} 

OrderSearch:

public function search($params) 
{ 
    // Set cookie with search params 
    Yii::$app->response->cookies->add(new \yii\web\Cookie([ 
     'name' => 's-' . Yii::$app->user->identity->id, 
     'value' => \yii\helpers\Json::encode($params['OrderSearch']), 
     'expire' => 2147483647, 
    ])); 

    // ... search model code here ... 
} 

PS:可以肯定的,如果你可以使用array_column爲對象的數組。 這適用於PHP 7+,但在較低版本中,您必須自行提取id。在PHP 5中使用array_walkarray_filter也許是個好主意。4+

相關問題