Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
258 views
in Technique[技术] by (71.8m points)

yii extensions - 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

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

<?=

    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. to make GoogleChart dynamic. How to echo the function returned value from model in view in yii2 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can try this code for getting value from model's functions.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...