2012-02-11 55 views
4

我遇到的問題與Yii中的單個控制器/視圖中的多個模型有關。 具體而言,我無法弄清楚如何使用Gii生成的CRUD在管理和搜索視圖中爲我的相關模型設置搜索欄。多個相關型號單個控制器

我有兩個型號「配方」和「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', 
     ), 
    ), 
)); ?> 

不過,我需要弄清楚如何在SE添加在「指令」字段上方的拱形條,以便可以搜索字段。

我需要弄清楚如何在表單中添加「說明」。在食譜

<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, 
     )); 

和指數和Admin中的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, 
    )); 
} 

我知道這應該是容易的,但我似乎無法弄清楚如何將我的頭圍繞它閱讀我能找到的每一份文件。

回答