0

I am currently in admin view consisitng of a table with foregin keys. Thes FK I have magaed to lookup and display the content of the key. I am trying to compare using the lookup values of the FK and not the FK id. The following is what I have but is not fully working but does give me some results. I need to compare lead_desc which is stored in the main table using FK and bring back all LIKE values.

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

1 回答 1

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

于 2012-12-19T13:43:03.607 回答