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(),那么只需在视图方法中调用它。

于 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 回答