多个相关型号单控制器
我遇到的问题与 Yii 中单个控制器/视图中的多个模型有关。 具体来说,我不知道如何在管理和搜索视图中使用 Gii 生成的 CRUD 为我的相关模型提供搜索栏。
我有两个模型“Recipes”和“RecipeSteps”
这是我的食谱关系,
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',
),
),
)); ?>
但是我需要弄清楚如何在“说明”字段上方的搜索栏中添加,以便也可以搜索该文件。
我需要弄清楚如何将“说明”添加到我的表单中。
<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 -->
食谱中的食谱搜索功能
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,
));
}
我知道这应该很容易,但我似乎不知道如何解决它,我已经阅读了我能找到的所有文档。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要更改搜索和列定义才能使用该关系。
您需要使用 with()、列过滤器()。
阅读此主题:
Yii - 通过 cgridview 搜索关系字段的过滤器
You need to alter your search and column definition to work with the relation.
You need to use with(), columns filter().
Read this topic:
Yii - Search filter of a relations field through cgridview