2012-06-21 44 views
0

我嘗試使用CSQLDataProvider獲取值,但出現錯誤消息。 未定義指數:ID K:\警予,1.1.8.r3324 \框架\網絡\ CSqlDataProvider.php(116)yii使用CSqlDataProvider獲取數據

這裏是我的代碼

車型

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); 
} 
} 

視圖

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

     ), 
)); 

回答

3

試試這個:

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

'productId' - 表Product

+0

的 「數據提供程序」 屬性id字段不能爲空。 – user510783

1
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); 
                  }' 
                ), 
              ), 
             ), 
            ), 
           ) 
          ); 
        ?>