duanpai6581 2015-12-20 22:23
浏览 377
已采纳

yii2模型函数中缺少参数id

I have these 2 methods for my previous and next records buttons in my task model

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

public function getPrev()
{
    return Task::find()
         ->where(['<', 'id', $this->id])
         ->orderBy(['id' => SORT_DESC])
         ->one();
}

this is working fine but after the last record and before 1 record I get this error Missing required parameters: id. how can I display a custom message instead of this error or disable/hide the next button when it reaches the last record

code in the view file

<?= Html::a('Previous',  
    ['/pmt/task/task-view', 'id' => $model->prev->id], 
    ['class'=>'btn btn-primary btn-xs']); 
?>
<?= Html::a('Next',  
    ['/pmt/task/task-view', 'id' => $model->next->id],
    ['class'=>'btn btn-primary btn-xs']); 
?>
  • 写回答

1条回答 默认 最新

  • doubi3996 2015-12-20 23:13
    关注

    Just put an if statement around the next button?

    <?= Html::a('Previous',  
      ['/pmt/task/task-view', 'id' => $model->prev->id], 
      ['class'=>'btn btn-primary btn-xs']); ?>
    <?php if($next = $model->next) {
      <?= Html::a('Next',  
        ['/pmt/task/task-view', 'id' => $next->id], 
        ['class'=>'btn btn-primary btn-xs']); ?>
    <?php } ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?