简体   繁体   English

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

[英]Yii2:how to get return value in view from model?

I have a table name "staff".Staff table has one to many relation with attendance table.我有一个表名“员工”。员工表与考勤表有一对多的关系。

In model Staff.php在模型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;
    }

in views report.php在视图report.php

<?=

    GoogleChart::widget(['visualization' => 'PieChart',
                'data' => [
                    ['Task', 'Hours per Day'],
                    ['Present', 5],
                    ['Absent', 2],
                    ['leave', 4],
                ],]);
?> 

i want to get the returned value of $present ,$Absent and $leave.我想获得$present ,$Absent and $leave.的返回值$present ,$Absent and $leave. to make GoogleChart dynamic.使 GoogleChart 动态化。 How to echo the function returned value from model in view in yii2 ?如何在 yii2 视图中回显模型的函数返回值?

You can try this code for getting value from model's functions.您可以尝试使用此代码从模型的函数中获取价值。

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

I think you should use static function我认为你应该使用静态函数

 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;
}

and the use in your widget以及在您的小部件中的使用

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()],
            ],]);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM