Search code examples
cakephpis-emptycakephp-3.2

empty() not working in cakephp


I have products table and want to select all products. This is what I have done

$products = $this->Products->find('all', [
   'conditions' => [
       'status' => 1
   ]
]);
$this->set('products', $products);

and print products if fetched and No product found when no product is retrieved.

This is what I have done for this

if (!empty($products)):
   // show products
else:
   echo 'No Products Found';
endif;

But this is not working, even if no product is found else condition is not printed.

If condition is not even working in controller action. Is there something missing ?

I'm using CakePHP 3.2


Solution

  • for check content that is null or not use function isEmpty() in CakePHP 3.0.5

    if (!$products->isEmpty()) {
       // show products  
    }
    else
       echo 'No Products Found';