. . .

Add conditions in search for relational tables

Published: April 9, 2011

On This Page

    Let’s discuss example relationship given on following URL

    http://www.yiiframework.com/doc/guide/1.1/en/database.arr

    If you want to be able to filter results in grid using Posts’ author name then modify the search function of  Post model to similar to the one given below.

    public function search()
    	{
    		$criteria=new CDbCriteria;
    
    		$criteria->compare('id',$this->id);
    
    		if(!intval($this->author_id) && is_string($this->author_id) && strlen($this->author_id) > 0) {
    			$criteria->with[]='user';
    		  	$criteria->addSearchCondition("user.name",$this->author_id,true);
    		}else{
    			$criteria->compare('author_id',$this->author_id);
    		}
    
    		return new CActiveDataProvider(get_class($this), array(
    			'criteria'=>$criteria,
    			'pagination' => array(
    				'pageSize'=>Yii::app()->params['defaultPageSize']
    			)
    		));
    	}

     

    Don't forget to share this post

      Let's Build Digital Excellence Together


      • Cost Efficient Solutions.
      • Minimal Timelines.
      • Effective Communication.
      • High Quality Standards.
      • Lifetime Support.
      • Transparent Execution.
      • 24/7 Availability.
      • Scalable Teams.

      Join Our 200+ Happy Clients Across Globe


      Free Consultation.

        Do you need tech help of your startup/business? Experts from our team will get in touch with you.

        Please do not post jobs/internships inquiries here.