mysql - Yii2 : Getting unknown property when using findBySql -
i'm using findbysql() data database, want show data in view table.
this code on controller :
$sql = "select presensi.presensi_tanggal 'tanggal', sum(if( hadir.keteranganhadir_id='1',1,0)) 'hadir', sum(if( hadir.keteranganhadir_id='2',1,0)) 'tidak_hadir', count(*) 'total' hadir, keteranganhadir, presensi hadir.keteranganhadir_id = keteranganhadir.keteranganhadir_id , hadir.presensi_id = presensi.presensi_id group presensi.presensi_tanggal"; $model = hadir::findbysql($sql)->all(); return $this->render('index', [ 'hadir' => $model, ]);
so, want show 'tanggal', 'hadir', 'tidak hadir' , 'total'.
in view,
<?php foreach($hadir $data): ?> <tr> <td></td> <td class="tbl_column_name"><?=$data->tanggal;?></td> <td class="tbl_column_name"><?=$data->hadir;?></td> <td class="tbl_column_name"><?=$data->tidak_hadir;?></td> <td class="tbl_column_name"><?=$data->total;?></td> <td><a href="kehadiran/view?id=1">lihat</a></td> </tr> <?php endforeach; ?>
but, got error unknown property – yii\base\unknownpropertyexception getting unknown property: common\models\data\hadir::tanggal. so, problem , should do? thank you~
[edited]
hadir model :
<?php namespace common\models\data; use yii; /** * model class table "hadir". * * @property integer $adikbinaan_id * @property integer $presensi_id * @property integer $keteranganhadir_id * * @property adikbinaan $adikbinaan * @property presensi $presensi */ class hadir extends \yii\db\activerecord { /** * @inheritdoc */ public static function tablename() { return 'hadir'; } /** * @inheritdoc */ public function rules() { return [ [['adikbinaan_id', 'presensi_id', 'keteranganhadir_id'], 'required'], [['adikbinaan_id', 'presensi_id', 'keteranganhadir_id'], 'integer'] ]; } /** * @inheritdoc */ public function attributelabels() { return [ 'adikbinaan_id' => 'adikbinaan id', 'presensi_id' => 'presensi id', 'keteranganhadir_id' => 'keteranganhadir id', ]; } /** * @return \yii\db\activequery */ public function getadikbinaan() { return $this->hasone(adikbinaan::classname(), ['adikbinaan_id' => 'adikbinaan_id']); } /** * @return \yii\db\activequery */ public function getpresensi() { return $this->hasone(presensi::classname(), ['presensi_id' => 'presensi_id']); } }
by default attributes extracted returned rows columns can found in table.
i impression code fields not. fix should override attributes()
-function , declare properties valid:
public function attributes() { return array_merge(parent::attributes(), ['tanggal', 'hadir', 'tidak_hadir', 'total']); }
this should instruct populaterecord()
-function fill those.
Comments
Post a Comment