[Solved]CSqlDataProvier with CGridView


I have 2 days trying to put in a table data from an SQL query from multiple tables, after reviewing several times in the documentation and forum, I have this code:


	public function actionIndex()


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



			->leftJoin('Inventario_Stocks', 'Inventario_Stocks.Inventario_idInventario=Inventario.idInventario')

			->leftJoin('Stocks', 'Inventario_Stocks.Stocks_idStocks=Stocks.idStocks')

			->leftJoin('Ubicaciones', 'Stocks.Ubicaciones_idUbicaciones=Ubicaciones.idUbicaciones')

			->leftJoin('TipoPieza', 'Inventario.Tipo_idTipoPieza=TipoPieza.idTipoPieza')


		$cantidad=Yii::app()->db->createCommand('SELECT COUNT(*) FROM Inventario')->queryScalar();

		$dataProvider=new CSqlDataProvider($sql, array(










		//$dataProvider=new CActiveDataProvider('Inventario');

		$model=new Inventario('search');

		$model->unsetAttributes();  // clear any default values









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


























I guess the dataProvider data are correct, if I put a field in $data[] is not correct gives me another error.

The error I get is the following:

[size="6"]Illegal offset type[/size]



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()

Anyone can tell me what wrong?

Thank you very much

"keyField" property of CSqlDataProvider must be a string:

'keyField' => 'idInventario',

Phtamas thank you very much, but do not quite understand.

How a string? Is not a key field in the table?, If I put ‘keyField’ => array (‘piece’), which is a VARCHAR (45) of the Inventory table, the same error keeps coming.

I regret my stupidity, but I’m already a little stubborn to give as many laps.

Sorry. Already, it’s like you have your job, thank you very much, is that it takes all day and it is that neither I