Hi,
I found a lot of similar topics, but no topic could help me.
I have two models, Data and Image. Data has many relations to Image.
I want to implement only one search for both models with CGridView.
DataController:
public function actionAdmin()
{
$modelData=new Data('search');
$modelData->unsetAttributes(); // clear any default values
if(isset($_GET['Data']))
$modelData->attributes=$_GET['Data'];
$this->render('admin',array(
'modelData'=>$modelData,
));
}
Data.php:
public function rules()
{
return array(
array('id, title, description, [b]image[/b]', 'safe', 'on'=>'search'),
);
}
public function relations()
{
return array(
[b]'image' => array(self::HAS_MANY, 'Image', 'data_id'),[/b]
);
}
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id,true);
$criteria->compare('title',$this->title,true);
$criteria->compare('description',$this->description,true);
[b]$criteria->compare('image',$this->image[0]->image,true);[/b]
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
admin.php:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'data-grid',
'dataProvider'=>$modelData->search(),
'filter'=>$modelData,
'columns'=>array(
'id',
'title',
'description',
'create_date',
[b]'image.0.image',[/b]
array(
'class'=>'CButtonColumn',
),
),
)); ?>
_search.php:
<div class="row">
[b]<?php echo $form->label($modelData,'image'); ?>
<?php echo $form->textField($modelData,'image[0]'); ?>[/b]
</div>
The output is correct (see attachment), but the search doesn’t work.
I tried a lot of other code snippets (e.g. $criteria->with(), etc.), but the same result.
I hope you can help me.
Thanks a lot.
Motte