Yii我怎么能用相关表搜索

时间:2014-06-17 18:38:19

标签: 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 个答案:

答案 0 :(得分:0)

$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>