In CakePHP 2
I always used empty
to check if there any result.
<?php
$result = $this->Modelname->find('first', ['conditions' => ['field' => 'value'] ] );
if ( empty($result) ) {
// Bad Request
}
In CakePHP 3
it looks weird to me.
$fancyTable = TableRegistry::get('FancyTable');
$query = $fancyTable->find()->where(['name' => 'fancy', 'active' => 0]);
if ( 0 === $query->count() ) {
// Bad Request
}
Is this the right way?
You can do:
$fancyTable = TableRegistry::get('FancyTable');
$exists = $fancyTable->exists(['name' => 'fancy', 'active' => false]);
Use something like this:
if ($query->isEmpty()) {
// Query or result set is empty
}
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