简体   繁体   中英

yii fetching data using CSqlDataProvider

I tried get values using CSQLDataProvider but I get an error Message. Undefined index: id K:\\yii-1.1.8.r3324\\framework\\web\\CSqlDataProvider.php(116)

Here is my code

models

class Product extends CActiveRecord{
...
public function getProductsForIndexPage(){
        $sql = '
            SELECT P.*  FROM Product P, Make M, Company C
            WHERE M.makeId=P.makeId AND C.companyId= P.companyId
        ';
        return new CSqlDataProvider($sql);
}
}

view

$model = new Product(); 
$this->widget('zii.widgets.grid.CGridView', array(
        'dataProvider'=>$model->getProductsForIndexPage(),
        'columns'=>array(
                'name'

        ),
));

Try this:

 return new CSqlDataProvider($sql, array('keyField' => 'productId'));

Where 'productId' - id field in table Product

Try this

In model

public function reportSearch()
    {
        $query = "SELECT * FROM (";

        $countQuery = 'SELECT count(*) FROM (';

        $qryBody = "
            SELECT p_id, name, sum(stock_qty) as stock_qty, sum(sales_qty) as sales_qty, (sum(stock_qty) - sum(sales_qty)) as stock_in_hand FROM 
            ( 
                SELECT p.id as p_id, p.name as name, 0 as stock_qty, 0 as sales_qty FROM jubal.product p where status != 'D'
                UNION 
                SELECT ps.product_id as p_id, '-' as name, sum(ps.qty) as stock_qty, 0 as sales_qty FROM jubal.product_stock ps 
                ".($this->shortDate ? "where ps.purchase_date <= '".Product::dateFormat($this->shortDate)."'" : "")." group by ps.product_id 
                UNION 
                SELECT si.product_id as p_id, '-' as name, 0 as stock_qty, sum(si.quantity) as sales_qty FROM jubal.sales_bill si where status!= 'D'
                ".($this->shortDate ? "AND si.sales_date <= '".Product::dateFormat($this->shortDate)."'" : "")." group by si.product_id
            ) ss GROUP BY p_id 
            ) res";

        $query .= $qryBody;
        /*print_r($query);
        exit();*/
        $countQuery .= $qryBody; 

        $condition = "";
        if(!empty($this->id)){
            $condition .= "p_id like'%". $this->id. "%'";
        }

        if(!empty($this->name)){
            $condition = (!empty($condition) ? $condition." and " : '');
            $condition .= "name like '%". $this->name. "%'";
        }

        if(!empty($this->stock_qty)){
            $condition = (!empty($condition) ? $condition." and " : '');
            $condition .= "stock_qty = '". $this->stock_qty. "'";
        }

        if(!empty($this->sales_qty)){
            $condition = (!empty($condition) ? $condition." and " : '');
            $condition .= "sales_qty = '". $this->sales_qty. "'";
        }

        if(!empty($this->stock_in_hand)){
            $condition = (!empty($condition) ? $condition." and " : '');
            $condition .= "stock_in_hand = '". $this->stock_in_hand. "'";
        }


        $query .= (!empty($condition) ? " where ". $condition : '');

        $countQuery .= (!empty($condition) ? " where ". $condition : '');


       /* print_r($query);
        exit();*/
       /* print_r($countQuery);
        exit();*/

        // SELECT p_id, name, sum(stock_qty) as stock_qty, sum(sales_qty) as sales_qty, (sum(stock_qty) - sum(sales_qty)) as stock_in_hand

        $count=Yii::app()->db->createCommand($countQuery)->queryScalar();
        return new CSqlDataProvider($query, array(
            'totalItemCount'=>$count,
            'keyField' => 'p_id',
            'sort'=>array(
                'attributes'=>array(
                     'p_id', 'name', 'stock_qty', 'sales_qty', 'stock_in_hand'
                ),
            ),
            'pagination'=>array(
                'pageSize'=> Yii::app()->user->getState( 'pageSize', Yii::app()->params[ 'defaultPageSize' ] ),
            ),
        ));
    }

In view page(cgridview)

<?php 
                        $this -> widget('booster.widgets.TbExtendedGridView', 
                            array(
                                'id' => 'Stock-report-grid',
                                'dataProvider' => $model -> reportSearch(),
                                'filter' => $model,
                                'type' => 'bordered condensed',
                                'htmlOptions'=>array('style'=>'0px'),
                                'columns' => 
                                    array(
                                        array(
                                            'header' => 'ID',
                                             'name'=>'p_id',
                                            'value'=>'$data["p_id"]',
                                            'type' => 'raw',
                                            // 'filterHtmlOptions'=>array('style'=>'width: 20%;')
                                            ),
                                        array(
                                            'header' => 'Name',
                                             'name'=>'name',
                                            'value'=>'$data["name"]',
                                            'type' => 'raw',
                                            // 'filterHtmlOptions'=>array('style'=>'width: 20%;')
                                            ),
                                        array(
                                            'header' => 'Stock',
                                             'name'=>'stock_qty',
                                            'value'=>'$data["stock_qty"]',
                                            'type' => 'raw',
                                            // 'filterHtmlOptions'=>array('style'=>'width: 20%;')
                                            ),
                                        array(
                                            'header' => 'Sales',
                                            'name'=>'sales_qty',
                                            'value'=>'$data["sales_qty"]',
                                            'type' => 'raw',
                                            // 'filterHtmlOptions'=>array('style'=>'width: 20%;')
                                            ),
                                        array(
                                            'header' => 'Current Quantity',
                                            'name'=>'stock_in_hand',
                                            'value'=>'$data["stock_in_hand"]',
                                            'type' => 'raw',
                                            // 'filterHtmlOptions'=>array('style'=>'width: 20%;')
                                            ),
                                        array(
                                            'class'=>'bootstrap.widgets.TbButtonColumn',
                                            'template'=>'{showStockHistory}',
                                            'buttons'=>array(
                                                'showStockHistory'=>array(
                                                        'label'=>'',
                                                        'url'=>'$data["p_id"]',
                                                        'options'=>array(
                                                            'title'=>'StockHistory',
                                                            'class'=>'glyphicon glyphicon-folder-open',
                                                            ),
                                                        'click'=>'js:function(evt){
                                                                 evt.stopPropagation();
                                                                 evt.preventDefault();
                                                                 var id = $(this).attr("href");
                                                                 showStockHistory(id);
                                                            }'
                                                    ),
                                            ),
                                        ),
                                    ),
                                )
                            );
                    ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM