2

我有products桌子,想选择所有产品。这就是我所做的

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

并在已获取No product found且未检索到产品时打印产品。

这就是我为此所做的

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

但这不起作用,即使没有找到产品,也不会打印其他条件。

如果条件甚至在控制器操作中都不起作用。有什么遗漏吗?

我正在使用 CakePHP 3.2

4

3 回答 3

7

用于检查内容为空或不使用isEmpty()CakePHP 3.0.5 中的函数

if (!$products->isEmpty()) {
   // show products  
}
else
   echo 'No Products Found';
于 2016-08-14T08:00:11.117 回答
0

如果您的 cakephp 是 3.0.5 或更高版本,请阅读ashkufaraz 的答案,否则,您可以使用:

if(count($products) > 0)
 {
     //Print ya products
 }
else
 {
    echo 'No products found';
 }
于 2016-08-16T17:01:11.317 回答
0

尝试这个..

if (!empty($products->toArray())):
   // show products
else:
   echo 'No Products Found';
endif;
于 2016-08-15T09:58:33.383 回答