Yii Framework Forum: Question about CActiveDataProvider criteria - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Question about CActiveDataProvider criteria Using the 'select' option. Rate Topic: -----

#1 User is offline   jogasa21 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 27
  • Joined: 09-March 11
  • Location:Costa Rica / Cambodia

Posted 27 July 2011 - 08:12 PM

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??
0

#2 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,359
  • Joined: 12-October 09
  • Location:Croatia

Posted 28 July 2011 - 02:52 AM

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...
Find more about me.... btw. Do you know your WAN IP?
0

#3 User is offline   jogasa21 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 27
  • Joined: 09-March 11
  • Location:Costa Rica / Cambodia

Posted 28 July 2011 - 03:07 AM

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?
0

#4 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,359
  • Joined: 12-October 09
  • Location:Croatia

Posted 28 July 2011 - 03:38 AM

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...
Find more about me.... btw. Do you know your WAN IP?
2

#5 User is offline   jogasa21 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 27
  • Joined: 09-March 11
  • Location:Costa Rica / Cambodia

Posted 28 July 2011 - 03:51 AM

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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users