The issue I'm having is related to multiple models in a single controller/view in Yii. Specifically I can't figure out how to have a search bar for my related model in the the admin and search views with Gii generated CRUD.
I have two models "Recipes" and "RecipeSteps"
This is my Recipes relations
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(
'recipeSteps' => array(self::HAS_ONE, 'RecipeSteps', 'recipe_id'),
);
}
I can already create and update using the related models the issue comes in under the search I can see the related model "RecipeSteps" in the results because I added it into my Gridview like so:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'recipes-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
//'recipe_id',
'recipe_name',
'recipe_description',
'recipeSteps.instructions',
array(
'class'=>'CButtonColumn',
),
),
)); ?>
However I need to figure out how to add in the search bar above the "instructions" field so that filed can be searched as well.
I need to figure out how to add "instructions" to my form.
<div class="wide form">
<?php $form=$this->beginWidget('CActiveForm', array(
'action'=>Yii::app()->createUrl($this->route),
'method'=>'get',
)); ?>
<div class="row">
<?php echo $form->label($model,'recipe_name'); ?>
<?php echo $form->textField($model,'recipe_name',array('size'=>11,'maxlength'=>11)); ?>
</div>
<div class="row">
<?php echo $form->label($model,'recipe_description'); ?>
<?php echo $form->textArea($model,'recipe_description',array('rows'=>6, 'cols'=>50)); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Search'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- search-form -->
Recipe Search Function in Recipes
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
//$criteria->compare('recipe_id',$this->recipe_id,true);
$criteria->compare('recipe_name',$this->recipe_name,true);
$criteria->compare('recipe_description',$this->recipe_description,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
and in Index and Admin in RecipesController
/**
* Lists all models.
*/
public function actionIndex()
{
$dataProvider=new CActiveDataProvider('Recipes');
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
/**
* Manages all models.
*/
public function actionAdmin()
{
$model=new Recipes('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['Recipes']))
$model->attributes=$_GET['Recipes'];
$this->render('admin',array(
'model'=>$model,
));
}
I know this should be easy but I can't seem to figure out how to wrap my head around it I have read every piece of documentation I could find.
You need to alter your search and column definition to work with the relation. You need to use with(), columns filter().
Read this topic: