Hi Guys,
I’ve been working with Yii now for a couple of weeks and it works like a charm! But now I’m doing some more complex Sql queries and I’m running into problem.
I have created a Sql query to fetch the total time spend on a project. The query works, I have tested it in MySql. I have added a function to the model that I want to use to fetch this data. Below you can find the code in the class.
Model
<?php
/**
* This is the model class for table "tbl_project_actie".
*
* The followings are the available columns in table 'tbl_project_actie':
* @property integer $id
* @property integer $project_id
* @property integer $actie_id
* @property string $tijd
*/
class ProjectActie extends CActiveRecord
{
public $total_time;
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return ProjectActie the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'tbl_project_actie';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('project_id, actie_id, total_time', 'required'),
array('project_id, actie_id', 'numerical', 'integerOnly'=>true),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, project_id, actie_id', 'total_time', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'project' => array(self::BELONGS_TO, 'Project', 'project_id'),
'actie' => array(self::BELONGS_TO, 'Actie', 'actie_id'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'project_id' => 'Project',
'actie_id' => 'Actie',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('project_id',$this->project_id);
$criteria->compare('actie_id',$this->actie_id);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
public function uren($project_id)
{
$criteria=new CDbCriteria;
$criteria->select = "t.project_id, SEC_TO_TIME(SUM(TIME_TO_SEC(u.tijd))) as total_time";
$criteria->join = "left join tbl_actie a on t.actie_id=a.id left join tbl_urenregistratie u on u.actie_id=t.actie_id";
$criteria->condition = "t.project_id=".$project_id." and a.status=3";
return self::model()->findAll($criteria);
}
}
Now I want to show the time I fetched in a view. I have found a working around, but I don’t think this is the way to do it.
<?php $time = ProjectActie::model()->uren($model->id);$uur = CHtml::listData($time , 'project_id','total_time');echo $uur[$model->id]?>
When I do the following nothing is shown
<?php echo ProjectActie::model()->uren($model->id)->total_time;
I can’t figure out why this doens’t work. Can someone please help?