Yannis P.
Yannis P.

Reputation: 833

Yii how to search for models (populate activedataprovider) with query result?

I use a search symptoms form and I pass off the symptomCode to my diseaseController

both symptoms and diseases have model classes, but the table connecting the 2 does not.

I use this query to find all the diseases that have the specific symptom

           $diseaseCodes = Yii::app()->db->createCommand()
                    ->select ('ICD10')
                    ->from('tbl_disease')
                    ->join('tbl_symptom_disease', 'tbl_disease.ICD10=tbl_symptom_disease.diseaseCode')
                    ->where('symptomCode=:symptomCode', 
                            array(':symptomCode'=>$_GET['symptomCode']))
                    ->queryAll();

Now I want to know how I can use this to populate a dataprovider to populate a gridview

One idea I had was to create a custom model function

public function queryResultSearch($diseaseArray)
{
    $criteria=new CDbCriteria;

    $criteria->compare('ICD10',$diseaseArray,true);

    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
    ));
}

And use this to render yii's admin action (for disease models), but I can't get it to work because probably my entire way at going at this is wrong.

Can someone help me please? How to use a mysql query result to populate an activedataprovider object.

Thank you for your time

Upvotes: 0

Views: 357

Answers (2)

TotPeRo
TotPeRo

Reputation: 6791

In your controller action you can try this:

    $model=new ModelName('search');
    $model->unsetAttributes();

    if(isset($_GET['ModelName']))
        $model->attributes=$_GET['ModelName'];

   $diseaseCodes = Yii::app()->db->createCommand()
        ->select ('ICD10')
        ->from('tbl_disease')
        ->join('tbl_symptom_disease', 'tbl_disease.ICD10=tbl_symptom_disease.diseaseCode')
        ->where('symptomCode=:symptomCode', 
                array(':symptomCode'=>$_GET['symptomCode']))
        ->queryAll();

    $diseaseArray=array();
    foreach ($diseaseCodes as $dc) {
        $diseaseArray[]=$dc['ICD10'];
    }

    $criteria=new CDbCriteria;
    $criteria->compare('ICD10',$diseaseArray,true);

    $dataProvider = new CActiveDataProvider(get_class($model),array('criteria'=>$criteria));
    $dataProvider->criteria->mergeWith($model->search()->criteria);
    $this->render('view',array(
            'model'=>$this->loadModel($id),
            'dataProvider'=> $dataProvider,
             ));

Upvotes: 1

Tim Rash
Tim Rash

Reputation: 11

You are going to want to use CArrayDataProvider. This is less than ideal.. and you will have to implement all your sorting and pagination on your own

You are much better off using a model and implementing similar to

public function queryResultSearch($diseaseArray)
{
    $criteria=new CDbCriteria;
    $criteria->compare('ICD10',$diseaseArray,true);
    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
    ));
 }

To make your first model I would personally use Gii.

Upvotes: 0

Related Questions