Is there a way to check if record exist in cake php . I know there is a function .CakePHP 2
$this->Notes->id = $id;
if (!$this->Notes->exists())
{
throw new NotFoundException(__('Invalid Notes'));
}
but by default it check with the column id
How can i check with a custom column suppose it is note_id
. my attemts are refer from here
attempt #1
if (!$this->Noteshistory->exists(['note_id'=>$id]))
{
throw new NotFoundException(__("Invalid Note "));
}
also tried to set note_id
$this->Noteshistory->note_id = $id;
if (!$this->Noteshistory->exists(['note_id'=>$id]))
{
throw new NotFoundException(__("Invalid Note "));
}
but no luck .
hasAny is the solution -
$this->Noteshistory->hasAny(['note_id'=>$id])
will return true
if found else false
hasAny is not available in version 3.x
you can use hasAny()
:-https://api.cakephp.org/2.6/class-Model.html#_hasAny
$conditions = array(
'note_id'=>$id
);
if ($this->Noteshistory->hasAny($conditions)){
//do something
}
Note:- not available in 3.x version
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With