0

私の問題は単純ですが、cakephp から始めています。私の問題を説明します:

私は2つのテーブルを持っています:

Table: Expenditures
Columns:
sub_category_id 
account_id  
date    
ammount     
created     
modified    
description

Table: BudgetRecords
Columns:
budget_id   
ammount     
sub_category_id 
created     
modified

私には「宿題」があり、先生は私にレポートを書くように求めています。このレポートは表を生成し、残高 (Expenditures.ammount - BudgetRecords.ammount) と各月 (BudgetRecords.created または Expenditures.date など) を表示します。

ここに私の質問があります: 私は foreach を実行しようとしており、年が存在する場合は各年 (存在する場合) を渡し、月ごとにクエリを実行します。しかし、CakePHP でそれを行うにはどうすればよいでしょうか? コントローラー内でのみこれを実行しようとしましたが、正常に動作します。しかし、昨年の最後の月だけを返します。それから、私は別のアプローチを試みました。

ビュー内でクエリを実行し、その年が存在するかどうかを検索します。存在する場合は、今月を検索します。その後、結果を返します。

それが私がしたことです:

ビュー内:

<table align="center" border="2" rules="All">
    <tr>
        <td>Valor:</td>
        <td>Data:</td>
    </tr>
    <?php
    for ($month = 1; $month <= 12; $month++) {
        if (strlen($month) == 1)
            $month = '0' . $month;

        foreach ($Expenditure->SaldoMes($month) as $result) {

            echo "<tr>";
            echo "<td> R$:" . $result[0] . "</td>";
            echo "<td> " . $result[1] . "</td>";
            echo "</tr>";
        }
    }
    ?>
</table>

コントローラーの内部:

public function SaldoMes($month = null) {
    $month = 0;
    for ($month = 1; $month <= 12; $month++) {
        if (strlen($month) == 1)
            $month = '0' . $month;
        $query = "SELECT (SUM(ex.ammount) - SUM(br.ammount)) as total , br.created as data
                    FROM budget_Records br, expenditure ex 
                    where SUBSTRING(br.created, 6, 2) = '" . $month .
                "' and SUBSTRING(br.created, 6, 2) = SUBSTRING(ex.created, 6, 2)";
        $this->set('Expenditure', $this->Expenditure->query($query));

        foreach ($this->Expenditure->query($query) as $records) {
            $this->set(('Total'), $records[0]['total']);
            $this->set(('Data'), $records['br']['data']);
        }
    }
}

誰かが私に解決策を示すのに十分なほど私の問題を説明できたことを願っています。

4

2 に答える 2

2

これが役立つと思います http://book.cakephp.org/2.0/en/controllers.html

この部分はあなたが探しているものだと思います。そのウェブサイトから取得しました。

<?php
# /app/Controller/RecipesController.php

class RecipesController extends AppController {
    public function view($id) {
        //action logic goes here..
    }

    public function share($customer_id, $recipe_id) {
        //action logic goes here..
    }

    public function search($query) {
        //action logic goes here..
    }
}

「これらのアクションのビュー ファイルは、app/View/Recipes/view.ctp、app/View/Recipes/share.ctp、および app/View/Recipes/search.ctp です。従来のビュー ファイル名は小文字で、アクション名の下線付きバージョン。"

したがって、SaldoMes() のような別のメソッドを使用したい場合は、view メソッドで呼び出すだけです。

于 2012-05-02T04:48:59.047 に答える
0

@earlonrails、解決済み:

public function SaldoMes() {
    $result = array();
    for ($month = 1; $month <= 12; $month++) {
        if (strlen($month) == 1)
            $month = '0' . $month;
        $query = "SELECT (SUM(ex.ammount) - SUM(br.ammount)) as total , br.created as data
                    FROM budget_Records br, expenditure ex 
                    where SUBSTRING(br.created, 6, 2) = '" . $month .
                "' and SUBSTRING(br.created, 6, 2) = SUBSTRING(ex.created, 6, 2)";
        $this->set('Expenditure', $this->Expenditure->query($query));
        foreach ($this->Expenditure->query($query) as $records) {
            $result[$month][0] = $records[0]['total'];
            $result[$month][1] = $records['br']['data'];
        }
    }
    return $this->set('result', $result);
}

$result 配列を使用して、値を取得できるようになりました。

助けてくれてありがとう。

于 2012-05-04T01:46:57.873 に答える