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