Hello,
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:
Controller:
public function actionIndex()
{
$sql = Yii::app()->db->createCommand()
->select('*')
->from('Inventario')
->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')
->text;
$cantidad=Yii::app()->db->createCommand('SELECT COUNT(*) FROM Inventario')->queryScalar();
$dataProvider=new CSqlDataProvider($sql, array(
'totalItemCount'=>$cantidad,
'sort'=>array(
'attributes'=>array('idInventario')
),
'keyField'=>array('idInventario'),
'pagination'=>array(
'pageSize'=>30,
),
));
//$dataProvider=new CActiveDataProvider('Inventario');
$model=new Inventario('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['Inventario']))
$model->attributes=$_GET['Inventario'];
$this->render('index',array(
'dataProvider'=>$dataProvider,
'model'=>$model,
));
}
view:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'inventario-grid',
'dataProvider'=>$dataProvider,
'columns'=>array(
array(
'name'=>'Nombre',
'type'=>'raw',
'value'=>'$data["pieza"]',
),
array(
'name'=>'Tipo',
'type'=>'raw',
'value'=>'$data["tipopieza"]',
),
array(
'name'=>'Ubicacion',
'type'=>'raw',
'value'=>'$data["ubicacion"]',
),
array(
'name'=>'Cantidad',
'type'=>'raw',
'value'=>'$data["cantidad"]',
),
),
));
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]
/opt/yii-1.1.7.r3135/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()
Anyone can tell me what wrong?
Thank you very much