Yii2:如何从模型中获取视图中的返回值?

Yii2:如何从模型中获取视图中的返回值?,yii2,yii-extensions,Yii2,Yii Extensions,我有一个表名“staff”。staff表与考勤表有一对多的关系 在modelStaff.php中 public function getAttendances() { if(isset($_GET['startdat'])) $start_date=$_GET['startdat']; if(isset($_GET['enddate'])) $end_date=$_GET['enddate']; if(is

我有一个表名“staff”。staff表与考勤表有一对多的关系

在model
Staff.php中

public function getAttendances()
    { 
        if(isset($_GET['startdat']))
        $start_date=$_GET['startdat'];
        if(isset($_GET['enddate']))
        $end_date=$_GET['enddate'];
        if(isset($_GET['startdat'])){
        return $this->hasMany(Attendance::className(), ['staff_id' => 'id'])
                ->where('daytime >= "'.$start_date.'" and daytime<="'.$end_date.'"');
        }
        else{
        return $this->hasMany(Attendance::className(), ['staff_id' => 'id'])
                ->andOnCondition(['daytime' => 'Absent'])
                ->orOnCondition(['status' => 'Present'])
                ->orOnCondition(['status' => 'leave']);
        }


    }
public function getPresent(){
        $present=0;
              foreach($this->attendances as $attendance){ 
                  if($attendance->status=='Present')
                    $present++; 
                  } 
              return $present;
    }


    public function getAbsent(){
        $Absent=0;
              foreach($this->attendances as $attendance){ 
                  if($attendance->status=='Absent')
                  $Absent++; 
              } 
              return $Absent;
    }
    public function getLeave(){
        $Leave=0;
              foreach($this->attendances as $attendance){ 
                  if($attendance->status=='Leave')
                  $Leave++; 
              } 
              return $Leave;
    }
公共功能GetAttentings()
{ 
如果(isset($\u GET['startdat']))
$start\u date=$\u GET['startdat'];
如果(isset($\u GET['enddate']))
$end_date=$_GET['enddate'];
如果(isset($\u GET['startdat'])){
return$this->hasMany(考勤::className(),['staff_id'=>'id']))

->其中('Daily>=“.$start\u date.”和Daily您可以尝试使用此代码从模型的函数中获取值

use path\to\model\Staff;
<?=
   GoogleChart::widget(['visualization' => 'PieChart',
                'data' => [
                    ['Task', 'Hours per Day'],
                    ['Present', Staff::getPresent()],
                    ['Absent', Staff::getAbsent()],
                    ['leave', Staff::getLeave()],
                ],]);
?>
使用path\to\model\Staff;

我认为应该使用静态函数

 public static function getAttendances()
 { 
  .......




 public static function getPresent(){
    $present=0;
          foreach(self::attendances() as $attendance){ 
              if($attendance->status=='Present')
                $present++; 
              } 
          return $present;
}


public static  function getAbsent(){
    $Absent=0;
          foreach(self::attendances() as $attendance){ 
              if($attendance->status=='Absent')
              $Absent++; 
          } 
          return $Absent;
}
public static function getLeave(){
    $Leave=0;
          foreach(self::attendances() as $attendance){ 
              if($attendance->status=='Leave')
              $Leave++; 
          } 
          return $Leave;
}
以及在小部件中的使用

use path\to\model\Staff;
<?php echo GoogleChart::widget(['visualization' => 'PieChart',
            'data' => [
                ['Task', 'Hours per Day'],
                ['Present', Staff::getPresent()],
                ['Absent', Staff::getAbsent()],
                ['leave', Staff::getLeave()],
            ],]);
?>
使用path\to\model\Staff;

它给出了错误“未定义的类常量'getAttentions'”。我使用静态函数作为'public static function getPresent(){$present=0;foreach(self::getAttentings as$attention){if($attention->status=='present')$present++;}return$present;}`仍然是相同的错误。我的问题是我认为
公共静态函数getPresent(){$present=0;foreach(static::attentings as$attenting){if($attenting->status=='present')$present++;}return$present;}
我已经用self::attentings()和static for getAttendaces()更新了答案try,但仍然给未定义的方法app\models\Staff::attentings()发出了此错误
调用
尝试调用self::getAttentings(),它会给出错误“未定义的类常量‘getAttentings’”。我使用静态函数作为“公共静态函数getPresent(){$present=0;foreach(self::getAttentings as$Attention){if($attention->status=='present')$present++;}返回$present;}`您可以尝试在
getAttentings()中添加静态关键字
as
公共静态函数getAttentings()
您使用“$this->getAttentings()”而不是“$this->Attentings()”仍然是相同的错误…实际上我在foreach循环中犯了错误。这是foreach循环
foreach(self::Attentings as$Attenting)
我不知道怎么了。。。
use path\to\model\Staff;
<?php echo GoogleChart::widget(['visualization' => 'PieChart',
            'data' => [
                ['Task', 'Hours per Day'],
                ['Present', Staff::getPresent()],
                ['Absent', Staff::getAbsent()],
                ['leave', Staff::getLeave()],
            ],]);
?>