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 R语言爬虫的时候元素和园代码不一样怎么解决呀
  • ¥15 VS2022多项目启动有问题
  • ¥15 SQL删除添加数据后序号不连续问题。
  • ¥15 首次运行OmniEvent运行报错
  • ¥15 有没有人知道这个问题怎么解决
  • ¥15 comsol电力电缆载流量仿真
  • ¥15 webSocket可以接TCP socket接口吗
  • ¥60 mpi并行出错,CFD++计算
  • ¥15 c#:vsto,powerpoint的外接程序中换主题颜色
  • ¥15 状态机/汽车转向灯/Sateflow