Query brings data ignoring $ conditions

1

I am making an inquiry and the cake is bringing all data ignoring $ conditions. I want to search by date, but when I put a date that does not exist in any record, it still brings all the data.

public function consultarDespesa() {
    if ($this->request->is('post')) {

        $dataDespesa=null;

        $dataDespesa = $this->request->data['Despesa']['ano']."-".$this->request->data['Despesa']['mes'];

        $despesas = $this->Despesa->find('all', array(
'conditions'=>array(
    'data_despesa like'=>"%$this->dataDespesa%")));

        if (($despesas==null) || (!isset($despesas))) {
            $this->Session->setFlash("Sua pesquisa não retornou nenhum resultado.");
        }

        else {
            $this->Session->write("Despesas", $despesas);

            $this->redirect(array('action'=>'exibirDespesas'));
        }
    }
}

link

    
asked by anonymous 19.01.2016 / 14:37

2 answers

1

You are comparing date with LIKE , in this case you will not get back any record, since no record is the same as the date you are looking for.

When you buy date for a particular day you can use >= and <= , between or other ways to compare dates.

I made it bigger and equal

$despesas = $this->Despesa->find('all', array(
    'conditions'=>array(
        'data_despesa >='=>"%$this->dataDespesa%",
        'data_despesa <='=>"%$this->dataDespesa%"
        )   
    ));

Edit - Try to compare this way:

$despesas = $this->Despesa->find('all', array(
    'conditions'=>array(
        'MONTH(data_despesa)'=>$this->request->data['Despesa']['mes'],
        'YEAR(data_despesa)'=>$this->request->data['Despesa']['ano']
        )   
    ));
    
19.01.2016 / 14:44
1

Change this:

'data_despesa like'=>"%$this->dataDespesa%"

To:

"data_despesa like '%{$this->dataDespesa}%'"
    
19.01.2016 / 14:43