Several related models of one controller

The problem I am facing is with multiple models in one controller / view in Yii. In particular, I cannot figure out how to create a search bar for my related model in the admin view and search using the generated Gii CRUD.

I have two models: Recipes and Recipes

This is my recipe relationship.

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 related models, the problem arises under the search. I see the associated RecipeSteps model in the results because I added it to my Gridview as follows:

 <?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 โ€œinstructionsโ€ in the search bar above the field so that I can search for the file 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, )); 

in both 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 figure out how to hug him around. I read every piece of documentation I could find.

+4
source share
1 answer

You need to change the search definition and column to work with this relation. You need to use with (), columns filter ().

Read this thread:

Yii - Filter search for relationship fields through cgridview

+3
source

Source: https://habr.com/ru/post/1395954/


All Articles