2015-09-14 3 views
-1

У меня есть имя таблицы «staff». Стол стол имеет отношение «один к большому» с таблицей посещаемости.Yii2: как получить возвращаемое значение с точки зрения модели?

В модели 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; 
    } 

в представлениях report.php

<?= 

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

я хочу, чтобы получить возвращаемое значение $present ,$Absent and $leave., чтобы сделать GoogleChart динамичным. Как эхо вернуть возвращаемое значение функции из модели в виде yii2?

ответ

0

Вы можете попробовать этот код для получения значения от функций модели.

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

это дает ошибка «Неопределенный класс константы„getattendances“» .И используется статическая функция, как 'public static function getPresent() {$ present = 0; foreach (self :: getattendances as $ attendance) {if ($ attendance-> status == 'Present') $ present ++; } return $ present; } ' – raxa

+0

вы можете попробовать добавить статическое ключевое слово в' getAttendances() 'as' public static function getAttendances() ' – GAMITG

+0

все равно он дает такую ​​же ошибку – raxa

0

Я думаю, вы должны использовать статическую функцию

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

дает ошибку «Неопределенная константа класса» getattendances ».i используется статическая функция как' public static function getPresent() { $ present = 0; foreach (self: getattendances as $ attendance) { if ($ attendance-> status == 'Present') $ present ++; } return $ present; } ' – raxa

+0

use' public static function getAttendances() {..... ' – scaisEdge

+0

все еще такая же ошибка. Моя проблема здесь, я думаю,' public static function getPresent() { $ present = 0; foreach (статические :: посещаемость как $ посещаемость) { if ($ attendance-> status == 'Present') $ present ++; } return $ present; } ' – raxa