Yii 多相关模型单控制器
我遇到的问题与Yii中单个控制器/视图中的多个模型有关。 具体来说,我不知道如何使用Gii生成的CRUD在管理和搜索视图中为我的相关模型设置搜索栏 我有两个模型“配方”和“配方步骤” 这是我的食谱Yii 多相关模型单控制器,yii,models,controllers,Yii,Models,Controllers,我遇到的问题与Yii中单个控制器/视图中的多个模型有关。 具体来说,我不知道如何使用Gii生成的CRUD在管理和搜索视图中为我的相关模型设置搜索栏 我有两个模型“配方”和“配方步骤” 这是我的食谱 public function relations() { // NOTE: you may need to adjust the relation name and the related // class name for the relations aut
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'),
);
}
我已经可以使用搜索下的相关模型创建和更新问题。我可以在结果中看到相关模型“RecipeSteps”,因为我将其添加到我的Gridview中,如下所示:
<?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',
),
),
)); ?>
在索引中,在控制器中,在管理中
/**
* 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,
));
}
我知道这应该很容易,但我似乎不知道该怎么做。我已经阅读了所有我能找到的文档 您需要更改搜索和列定义以处理该关系。 您需要使用with(),columns filter() 阅读本主题:
我一直在寻找这个,感谢通过yii网格视图筛选/搜索关系的伟大链接。
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,
));
/**
* 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,
));
}