yii如何比较外键

时间:2012-12-17 15:05:10

标签: search yii

我目前在管理视图中使用foregin键来表示一致。这些FK我已经开始查找并显示密钥的内容。我试图使用FK的查找值而不是FK id进行比较。以下是我所拥有的,但没有完全发挥作用,但确实给了我一些结果。我需要使用FK比较存储在主表中的lead_desc并返回所有LIKE值。

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

        $criteria=new CDbCriteria;

        $criteria->compare('leads_id',$this->leads_id);
        $criteria->compare('date_of_entry',$this->date_of_entry,true);
        $criteria->compare('lead_input',$this->lead_input,true);
        $criteria->compare('account_name',$this->account_name,true);
        //$criteria->compare('lead_desc',$this->lead_desc);

        if(  !empty($this->lead_desc) && is_string($this->lead_desc)){

            $match = addcslashes($this->lead_desc, '%_'); // escape LIKE's special characters       
            $sub_criteria = new CDbCriteria;
            $sub_criteria->condition = "name LIKE :match";
            $sub_criteria->params = array(":match" =>  "%$match%" );

            $row = LeadDesc::model()->findAll($sub_criteria);
            $arr = array(); //no tused
            if($row){
                foreach ($row as $key => $value) {              
                    $arr[$value->lead_desc_id] = $value->name; //not used
                    //collect all records in leads table and merge with current search
                    $criteria->compare('lead_desc',$value->lead_desc_id,true,'OR');                 
                }
                //print_r($arr);
                //exit();

                //$criteria->mergewith
            }else{ 
                $this->lead_desc = null;
            }

        }

        $criteria->compare('size',$this->size,true);
        $criteria->compare('lead_source',$this->lead_source);
        $criteria->compare('country',$this->country);
        $criteria->compare('region',$this->region);
        $criteria->compare('date_of_activity',$this->date_of_activity,true);

        //$criteria->compare('status',$this->status);
        if( !empty($this->status) && is_string($this->status)){
            $sub_criteria = new CDbCriteria;
            $sub_criteria->condition = "(name = :name)";
            $sub_criteria->params = array(":name" =>  trim($this->status) );

            // Get fixed asset id via asset number
            $row = Status::model()->find($sub_criteria);
            if($row){
                $criteria->compare('status',$row->status_id);
                $this->status = $row->name;
            }else{ 
                $this->status = null;
            }
        }


        $criteria->compare('assigned',$this->assigned);
        $criteria->compare('sales',$this->sales,true);
        $criteria->compare('comments',$this->comments,true);

        return new CActiveDataProvider($this, array(
            //'criteria'=>$criteria,
            'criteria'=>$criteria,
            'sort'=>array(
                    'defaultOrder'=>'leads_id DESC'
            ),
            'pagination'=>array(
                    'pageSize'=>15,
            ),
        ));
    }

1 个答案:

答案 0 :(得分:1)

像这样:

$criteria->with=array('related_model');
$criteria->compare('related_model.field',$this->related_model);

请记住,您需要正确配置模型关系: http://www.yiiframework.com/doc/guide/1.1/en/database.arr