0
I need to take the month and the year twice: to consult the expenses and calculate the amount of expenses found.
In the function consultarDespesas
, step for the two global variables to subsequently access them in the function quantidadeDespesas
. The $this->request->data['Despesa']['ano'] e $this->request->data['Despesa']['mes']
stay null
. I don’t know how to access anywhere in the controller Expenses this requisition.
So I chose to take the month and year soon, but still the two global variables are null when the program arrives at function quantidadeDespesas
.
Already debugged the two global variables or attributes, already debugged $this->request->data
and $this->data
, but it’s no use, everything is null
. Let’s see the codes:
class DespesasController extends AppController {
public $helpers=array('Form', 'Html');
public $ano;
public $mes;
public $uses = array('Despesa', 'Receita');
function to search for expenses:
public function consultarDespesa() {
if ($this->request->is('post')) {
$this->ano = $this->data['Despesa']['ano'];
$this->mes = $this->data['Despesa']['mes'];
$despesas = $this->Despesa->find('all',
array(
'conditions'=>array(
'YEAR(data_despesa)'=>$this->request->data['Despesa']['ano'],
'MONTH(data_despesa)'=>$this->request->data['Despesa']['mes'])));
if ($despesas == null) {
$this->Session->setFlash("Sua pesquisa não retornou nenhum resultado.");
}
else {
$this->Session->write("Despesas", $despesas);
$this->quantidadeDespesas();
$this->redirect(array('action'=>'exibirDespesas'));
}
}
}
Function to count how many expenses incurred:
public function quantidadeDespesas() {
$quantidadeDespesas = $this->Despesa->query("select count(id_despesa) as quantidadeDespesas from despesas where year(data_despesa)='$this->ano' and MONTH(data_despesa)='$this->mes'");
return $quantidadeDespesas;
}
You have debugged before if( $this->request->is( ...? Checks if the form method is even post.
– Marcos Xavier
You can also use Cake Count even if you set the conditions http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#find-Count
– Marcos Xavier