0

Yiiフレームワークで小さなアプリケーションを作成しようとしていますが、一瞬理解できません。CActiveRecordから拡張されたOrderモデルがあり、インデックスアクションでOrderモデルを2回使用しようとしましたが、問題が発生しました。

その理由は、ページ化されたレコードをテーブルに表示するためであり、最後の行にいくつかのフィールドの平均合計を表示したいのですが、この合計は、並べ替え/フィルター基準でフィルター処理されたすべてのレコードについて計算する必要があります

アクションのコードは次のとおりです。

public function actionIndex()
{
    $model = new Order('search');
    $model->unsetAttributes();
    if (isset($_GET['Order']))
        $model->attributes = $_GET['Order'];

    // Get average totals by current filters
    $totals = array();
    $tm = clone $model;
    $tmdp = $tm->search();
    $tmdp->criteria->select = array('avg(price) as avgPrice', 'avg(cost) as avgCost');
    $data = $tmdp->getData(true);
    $totals['cost'] = number_format($data[0]->avgCost, 0, '.', ' ');
    $totals['price'] = number_format($data[0]->avgPrice, 0, '.', ' ');
    $totals['marja'] = Order::getMarjaVal(round($data[0]->avgCost, 0), round($data[0]->avgPrice, 0));

    $this->render('index', array(
        'model'         => $model,
        'totals'        => $totals
    ));
}

私の見解では、同じ方法でデータを取得しています$ data = $ model-> search()-> getData();

したがって、最初のページを表示すると、すべて正常に機能しますが、ページを変更すると、$ data = $ tmdp-> getData(true); 空の配列を取得しています。また、次のように$tmdpのページを設定しようとしました。

    $tmdp->criteria->select = array('avg(price) as avgPrice', 'avg(cost) as avgCost');
    $tmdp->pagination->setCurrentPage(0);
    $data = $tmdp->getData(true);

しかし、この後、最初のページからのみビューにデータを取得します。

誰かがこれを正しく行う方法を教えてもらえますか?

4

1 に答える 1

0

ページネーションで現在のページを変更すると、Yiiは$ _GET ['Order_page']値を自動的に更新し(この値が設定されていない場合は作成されます)、データプロバイダーからデータを取得すると、ページ付けはこの値を自動的に取得します。したがって、このコードでは、このような変更を加えました。

public function actionIndex()
{
    $model = new Order('search');
    $model->unsetAttributes();
    if (isset($_GET['Order']))
        $model->attributes = $_GET['Order'];

    // Get average totals by current filters
    $totals = array();

    $tm = clone $model;
    $tdp = $tm->search();
    $tdp->criteria->select = array('avg(price) as avgPrice', 'avg(cost) as avgCost');
    if (isset($_GET['Order_page'])) {
        $curPage = $_GET['Order_page'] - 1;
    } else {
        $curPage = 0;
    }
    $tdp->pagination->currentPage = 0;
    $data = $tdp->getData(true);
    $totals['cost'] = number_format($data[0]->avgCost, 0, '.', ' ');
    $totals['price'] = number_format($data[0]->avgPrice, 0, '.', ' ');
    $totals['marja'] = Order::getMarjaVal(round($data[0]->avgCost, 0), round($data[0]->avgPrice, 0));
    $tdp->pagination->currentPage = $curPage;

    $this->render('index', array(
        'model'         => $model,
        'totals'        => $totals
    ));
}

したがって、$ _ GETグローバル配列からページを取得しようとし、owr操作の後、この値を更新し直します。

于 2012-08-01T16:59:46.220 に答える