Question about CActiveDataProvider criteria

Hi all

I just wonder what the ‘select’ clause does in the criteria of a CActiveDataProvider object. As far as I know it should filter the columns for the result set, but it does not do so. When I display the result set in a CGridView all of the table columns are shown. I had to add the ‘columns’ array to my CGridView to filter the columns I wanted to show.

This is the original code in my view:




<?php

 if (isset($_POST['contact_id'])) {

	$id = $_POST['contact_id'];

	$donationModel = new CActiveDataProvider('Donation', array(

   	'criteria'=>array(

      	'select'=>'dateOfVisit, donationType, amount, blood_type, note, project',

      	'condition'=>'contact_id= :id',

      	'params'=>array(':id' => $id)

   	)

	));

	$donationModel->getData(false);

	

	echo '<div id="donationDetail">';

	if ($donationModel->getTotalItemCount(false) > 0 ) {

   	$this->widget('zii.widgets.grid.CGridView', array(

      	'dataProvider'=>$donationModel,

   	));

	}

	echo '</div>';

 }

?>



In order to show the results as I wanted I added the columns array in the CGridView:




<?php

 if (isset($_POST['contact_id'])) {

	$id = $_POST['contact_id'];

	$donationModel = new CActiveDataProvider('Donation', array(

   	'criteria'=>array(

      	'select'=>'dateOfVisit, donationType, amount, blood_type, note, project',	// Does this work? what am I missing here to filter columns?

      	'condition'=>'contact_id= :id',

      	'params'=>array(':id' => $id)

   	)

	));

	$donationModel->getData(false);

	

	echo '<div id="donationDetail">';

	if ($donationModel->getTotalItemCount(false) > 0 ) {

   	$this->widget('zii.widgets.grid.CGridView', array(

      	'dataProvider'=>$donationModel,

      	'columns'=>array('dateOfVisit', 'donationType', 'amount', 'blood_type', 'note', 'project'),   // this should not be necessary, since I put a select in the criteria.

   	));

	}

	echo '</div>';

 }

?>



I tried using string and array in the ‘select’ clause in the criteria but with the same results.

So, what’s the point of having a ‘select’ in the CActiveDataProvider criteria??

NOTE: are you using Yii 1.0.x a, as you posted in that section ?

With CDbCriteria select you choose which fields you want the SELECT command to return…

With CGridView columns you choose what fields, in what order and how to display…

Those are two different things…

Hi mdomba

I am sorry if I posted in the wrong section. I am using Yii 1.1.6 and … yes !! I wanted the CActiveDataProvider to return only a few columns from the referenced table (‘Donation’) and not all of them. Why it does not happen like that?

When I show the results from the SELECT all of the table columns are shown in the CGridView. That’s why I had to add the ‘columns’ option in the CGridView.

What am I doing wrong?

NOTE: moved to proper sub-forum

Yes, the gridview displays all the columns as it reads the table schema… but the columns not selected dont have any value… so you get an empty column…

By current implementation to solve this… there is no other way than to set the columns in CGridView…

Right !.. I forgot to mention that important detail (the empty columns shown).

Thank you for your quick reply and because now I learned one more thing about Yii.