Cactivedataprovider Odd Behavior

So I have a situations where the CActiveDataProvider does or does not return the proper data with little change in the code.

In my model I have a filter so as to only return a specific set of elements


public function byLocation($locIndex)

{

	$this->getDbCriteria()->mergeWith(array(

		'condition' => 'location = :pLocIndex',

		'params' => array(':pLocIndex' => $locIndex),

	));

	return $this;  //This is wrong. I am not sure what I properly intend to return here, if anything.

}



This will return a list of plants that are in a specific location. I have gotten this to work in conjunction with a CActiveDataProvider in the following manner.


$pltDome = new CActiveDataProvider(Plants::model()->byLocation(9));

The problem that I am having is this.




$pltDome = new CActiveDataProvider(Plants::model()->byLocation(9));

$pltBlk =  new CActiveDataProvider(Plants::model()->byLocation(10));



Based on the DB table the first line should return 5 records and the second line should return 0 records.

When used in the above configuration both $pltDome and $pltBlk return zero records. If I insert


echo Yii::trace(CVarDumper::dumpAsString($pltDome->itemCount),'vardump');

between the two lines $pltDome returns the correct data set.

Now if I change the DB so that the $pltBlk returns at least one record somethings interesting happens.

With the second configuration and NOT having the trace between the two lines causes both data providers to return the same data set, which is the data set that properly should be returned with the $pltBlk and if I reverse the order then both data providers are populated with the $pltDome data set. So which ever CActiveDataProvdier is last overrides the filter from byLocation()

Now If I put the trace in between the lines of code then both data providers return the correct data set.

Given that the trace in between the two lines of code returns the correct data provider leads me to the idea that the first record set is being set in some manner before the second data providers overwrites the byLocation() functionality.

Given that I would like to use the data provider to provide to separate list from the same model to use in two different CListView(s) what do I need to change to provide this.

I know the CActiveDataProvider can provide the required data but I am having issues reusing the byLocation() filter.

Dear Friend

Would you please try the following?




public function byLocation($locIndex)

{

	$this->getDbCriteria()->mergeWith(array(

		'condition' => 'location = :pLocIndex',

		'params' => array(':pLocIndex' => $locIndex),

	));

	return $this->dbCriteria;

}




Then




$pltDome = new CActiveDataProvider('Plants',array(

'criteria'=>Plants::model()->byLocation(9),


));



Regards.

So the above was giving the same result, the last CActiveDataProvider being set with the byLocation(n) was controlling the data being served to the CListView(s) regardless of the n that was used when the CActiveDataProvider was declared in the Controller.

This lead me to the thinking that the


$pltDome = new CActiveDataProvider(Plants::model()->byLocation(9));

$pltBlk = new CActiveDataProvider(Plants::model()->byLocation(10));

are using the same underlying object and the CDbCriteria is being reset based on the last one seen.

With that in mind and creating two separate object for each CListView and a review of some posts.

Multiple CActiveDataProvider in one view for the same model - scopes applied late

Lead to this solution.




$tDome = New Plants;

$pltDome = new CActiveDataProvider($tDome->byLocation(9));

$tBlk = new Plants;

$pltBlk = new CActiveDataProvider($tBlk->byLocation(10));



This gives me two separate object to drive two separate CActiveDataProviders.

Dear Friend

Just make a small change.




public function byLocation($locIndex)

{

        $this->getDbCriteria()->mergeWith(array(

                'condition' => 'location = :pLocIndex',

                'params' => array(':pLocIndex' => $locIndex),

        ));

        return clone $this->dbCriteria;//here comes the change.

}







$pltDome = new CActiveDataProvider('Plants',array(

'criteria'=>Plants::model()->byLocation(9),


));


$pltDome = new CActiveDataProvider('Plants',array(

'criteria'=>Plants::model()->byLocation(10),


));



I have understood the problem completely now.

I checked the things in my localhost and It is working as expected.

Regards.