How do I query if a record exists in cakephp 2.6?

Asked

Viewed 194 times

0

I wonder if a record exists in cakephp 2.6 As everyone knows, cakephp has to use the id key, however this table is a key composed of 4 fields.

I gave a read and tried to use beforeSave(), checkUnique and could not make it work.

  • I’m a beginner, any instruction is welcome.

Thank you in advance for your help.

1 answer

0


I needed to check if there was a record where the key is 4 fields.

I got it sorted out. Here’s what I did.


I left it at the end of my model

public function beforeSave($options = array())
{
    $any = $this->find('first', array(
        'conditions' => array(
        'Recibo.funcionario_id' => $this->data['Recibo']['funcionario_id'],
        'Recibo.loja_id' => $this->data['Recibo']['loja_id'],
        'Recibo.ano' => $this->data['Recibo']['ano'],
        'Recibo.mes' => $this->data['Recibo']['mes'])));
    if($any)
    {
        return false;
    }else 
    {
        return true;
    }
}

and left a message on the controller to warn that it did not include.

if ($this->Recibo->save($this->request->data)) {
                    $recibo_id=$this->Recibo->getInsertId();
                    foreach($this->request->data['ItenRecibo'] as $person) 
                    {
                    //loop for each person added
                        $person['recibo_id']=$recibo_id;
                        $this->Recibo->ItenRecibo->create();
                        $this->Recibo->ItenRecibo->save($person);
                    }//end foreach
                    $this->Session->setFlash('Recibo salvo no BD'); 
                $this->redirect(array('action' => 'index'));                
            }else
            {
                $this->Session->setFlash('Recibo já cadastrado para esse funcionário nesta data!');
            }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.