Undefined index: id - PHP Error in CSqlDataProvider

Hey guys,

I use the following code in query data:




			$sql='

			SELECT tb_comment.* FROM tb_user, tb_image, tb_comment 

			WHERE 

			tb_user.idUser=1 AND 

			tb_user.idUser=tb_image.idUser AND 

			tb_image.idImage = tb_comment.idImage AND

			tb_comment.idWriter != 1

			';

			$count=Yii::app()->db->createCommand($sql)->queryScalar();

			$recentCommentDataProvider = new CSqlDataProvider($sql, array(

			    'totalItemCount'=>$count,

			    'sort'=>array(

				'attributes'=>array(

				     'dateCreated',

				),

			    ),

			    'pagination'=>array(

				'pageSize'=>10,

			    ),

			));



the data is rendered in the following view:





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

			 	'dataProvider'=>$recentCommentDataProvider,

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

				'itemView'=>'../comment/_viewLatestComment', 

				'emptyText'=>'Nothing',

				'viewData'=>array('isAdmin'=>0),

				

			));

Unfortunatly I get the following error:




PHP Error


Undefined index: id


/var/www/yii/framework/web/CSqlDataProvider.php(116)



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.

THX

Andreas

Anybody any idea?

Here is a more detailed error message:





PHP Error


Undefined index: id


/var/www/yii/framework/web/CSqlDataProvider.php(116)


104 

105         return $command->queryAll();

106     }

107 

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     }

119 

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]

[font="Times"][size="2"]




$recentCommentDataProvider = new CSqlDataProvider($sql, array(

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

	//...

));



[/size][/font]




$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


/var/www/ddd/protected/views/comment/_viewLatestComment.php(6)




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

05             <?php 

06                 echo $data->comment;

07                         

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.

[/size][/font]




echo $data['comment'];



[/size][/font]

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…