Undefined index: id - PHP Error in CSqlDataProvider

Hey guys,

I use the following code in query data:


			SELECT tb_comment.* FROM tb_user, tb_image, tb_comment 


			tb_user.idUser=1 AND 

			tb_user.idUser=tb_image.idUser AND 

			tb_image.idImage = tb_comment.idImage AND

			tb_comment.idWriter != 1



			$recentCommentDataProvider = new CSqlDataProvider($sql, array(











the data is rendered in the following view:

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


				'summaryText'=>"",//"{start}-{end} out of {count} uploads ",






Unfortunatly I get the following error:

PHP Error

Undefined index: id


I already found this topic http://www.yiiframework.com/forum/index.php?/topic/12389-csqldataprovider-undefined-index-id/ but didn’t know how to apply it to my scenario.



Anybody any idea?

Here is a more detailed error message:

PHP Error

Undefined index: id



105         return $command->queryAll();

106     }


108     /**

109      * Fetches the data item keys from the persistent data storage.

110      * @return array list of data item keys.

111      */

112     protected function fetchKeys()

113     {

114         $keys=array();

115         foreach($this->getData() as $i=>$data)

116             $keys[$i]=$data[$this->keyField];

117         return $keys;

118     }


120     /**

121      * Calculates the total number of data items.

122      * This method is invoked when {@link getTotalItemCount()} is invoked

123      * and {@link totalItemCount} is not set previously.

124      * The default implementation simply returns 0.

125      * You may override this method to return accurate total number of data items.

126      * @return integer the total number of data items.

127      */

128     protected function calculateTotalItemCount()

[font="Times"][size="2"][font="Arial"][size=2]Try this[/size][/font]:[/size][/font]


$recentCommentDataProvider = new CSqlDataProvider($sql, array(

	'keyField' => 'idComment', // guessed from your naming conventions




$sql ='SELECT * FROM tb_user u LEFT JOIN (tb_image i,tb_comment c) ON (u.idUser = i.idUser  AND i.idImage = c.idImage) WHERE u.idUser = 1 AND c.idWriter !=1';


AH Great!

That was it… you even guessed the name right :wink:

However now I get the following error in my view file:

(The code worked without any problems when using CActiveDataProvider)

PHP Error

Trying to get property of non-object


04         <div style="width:65px; float:left" id="footer_left">

05             <?php 

06                 echo $data->comment;


08             ?>            

09         </div>

[font="Times"][size="2"][font="Arial"][size=2]CSqlDataProvider::getData() returns an array of arrays. Fields can be accessed as array elements.


echo $data['comment'];


OK THX… yeah that works…

but know all my relational AR stuff doesn’t work anymore… such as: $data[‘RelationName’]->ForeignAttribute

is there a workaround?

thank you very much bro u helped me a lot…