2012-12-17 61 views
0

我目前在管理視圖consisitng表與foregin鍵。 Thes FK我已經找到並顯示密鑰的內容。我試圖比較使用FK的查找值而不是FK ID。以下是我擁有但尚未完全工作,但確實給了我一些結果。我需要比較使用FK存儲在主表中的lead_desc,並返回所有LIKE值。yii如何比較foregin密鑰

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

像這樣:

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

記住,您需要正確配置模型關係: http://www.yiiframework.com/doc/guide/1.1/en/database.arr

+0

這將比較ID至極是intger值。我需要它來比較字符串值,並帶回MySQL中的LIKE條件 – shorif2000