I’m new to Yii and it seems like an amazing tool, but I’ve been struggling for hours trying to get what would seem to be a simple task accomplished. I have two tables “Unit” and “EquipmentModel”. EquipmentModel is the parent table and “Unit” is the child. In the CGridview, for the EquipmentModels table, I would like to simply display a count of the Units associated with each EquipmentModel. This should take a few minutes in PHP alone, but I just can’t get it to work in Yii. Everything is set up stock. I suspect I’ve left out something obvious to a more experienced Yii coder.
Some snippets from EquipmentModel.php
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(
'units' => array(self::HAS_MANY, 'Unit', 'modelId'),
);
}
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('model', $this->model, true);
$criteria->compare('description', $this->description, true);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
And from the associated admin.php:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'equipment-model-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
// 'id',
'model',
'description',
[b]array('name' => 'units','value' => 'count($model->units)'),[/b]
array(
'class'=>'CButtonColumn',
),
),
)); ?>
I get error “htmlspecialchars() expects parameter 1 to be string, array given” I’ve tried all kinds of variations. Please help me before I go bannanas!