私の問題は単純ですが、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']);
}
}
}
誰かが私に解決策を示すのに十分なほど私の問題を説明できたことを願っています。