duanpai6581 2015-12-21 06: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-21 07: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 } ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何删除这个虚拟音频
  • ¥50 hyper默认的default switch
  • ¥15 网站打不开,提示502 Bad Gateway
  • ¥20 基于MATLAB的绝热压缩空气储能系统代码咨询
  • ¥15 R语言建立随机森林模型出现的问题
  • ¥20 unity内置语言切换的按钮设置
  • ¥15 中级微观经济学,生产可能性边界问题
  • ¥15 TCP传输时不同网卡传输用时差异过大
  • ¥15 请各位看看我写的属于什么算法,或者有更正确的写法?
  • ¥15 html5 qrcode 扫描器