PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, March 15, 2022

[FIXED] toArray give error in cakephp 3.0

 March 15, 2022     cakephp-3.0     No comments   

Issue

I want to get all product related to 'products' table. But when $product_key blank ,the code will give errors. I want the blank array without elements. My code in the controller like that.

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


Solution

You should put the {$ProductData} inside if condition like this:

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


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


Answered By - amar9312
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing