Yii как я могу выполнять поиск по связанной таблице

#php #yii

#php #yii

Вопрос:

 public function search()
    {
        // @todo Please modify the following code to remove attributes that should not be searched.

        $criteria=new CDbCriteria;
        $criteria->compare('id',$this->id);
        $criteria->compare('name',$this->name,true);
        $criteria->compare('short_description',$this->short_description,true);
        $criteria->compare('long_description',$this->long_description,true);
        $criteria->compare('stars',$this->stars);
        $criteria->compare('image',$this->image,true);
        $criteria->compare('price',$this->price);
        $criteria->compare('discount_price',$this->discount_price);
        $criteria->compare('count_orders',$this->count_orders);
        $criteria->with = array('categories');
        $criteria->compare('categories.id',$this->parent_cat, true);
        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }


<fieldset>
        <label>
            <div class="col-md-3">
                <?php echo $form->label($model->parent_cat,'parent_cat'); ?>
            </div>
            <div class="col-md-3">
                <?php echo $form->dropDownList($model,'parent_cat', CHtml::listData(Categories::model()->findAll(), 'parent_cat', 'name')); ?>
            </div>
        </label>
    </fieldset>


return array(
            'categories'=>array(self::BELONGS_TO, 'Categories', 'parent_cat'),
        );
  

Я не могу комбинировать категории с продуктом в форме поиска. Спасибо!

Ответ №1:

 $criteria->compare('parent_cat',$this->parent_cat);

<div class="col-md-3">
<?php 
  $Categories = Categories::model()->findAll();
  $listData = CHtml::listData($Categories,'id','name');
  $listData[0] = "Категория продукта";
  ksort($listData);
  echo $form->dropDownList($model,'parent_cat',$listData, array('class'=>'form-  control','options' => array($model->parent_cat=>array('selected'=>true)))); 
?>
</div>