toArray 在 cakephp 3.0 中给出错误

toArray give error in cakephp 3.0

我想获取与 'products' table 相关的所有产品。但是当 $product_key 空白时 ,代码会报错。我想要没有元素的空白数组。我在控制器中的代码就是这样。

$ProductTable        = TableRegistry::get('products');    
$ProductData = $ProductTable->find('All') ->where(['product_key' => $product_key]) ->first() ->toArray();

如果条件如下:

您应该将 {$ProductData} 放入其中:
$ProductTable        = TableRegistry::get('products');    
$ProductData = $ProductTable->find('All') ->where(['product_key' => $product_key]) ->first();


if($ProductData){
          $ProductData = $ProductData->toArray();
      }else{
          $ProductData = [];
      }