2

I am working on CakePHP 3.2

I have this function to find product types if exists

if ($product['product_type'] != null) {
   $getProductType = $this->ProductTypes->find()
     ->where(['title LIKE' => '%'.$product['product_type'].'%'])
     ->first();

   debug($product['product_type']);   // this is not empty
   debug($getProductType);            // this shows 'id' => 1 in array
   debug($getProductType->id);        // this shows 1

   if (!empty($getProductType)) {
      $p->product_type_id = $getProductType->id;           // line 11
      $p->subcategory_id = $getProductType->subcategory_id;
      $p->category_id = $getProductType->category_id;

      return $this->saveNewBulkProduct($product, $p->category_id, $p->subcategory_id, $p->product_type_id);
   }
}

But this is giving warning as

Creating default object from empty value on line 11

Edit 2 Output of print_r($getProductType)

App\Model\Entity\ProductType Object 
( 
   [id] => 3 
   [category_id] => 1
   ....
)
Gaurav
  • 131
  • 12

2 Answers2

2

I think no need to declare $p and not set property and its value in $p, you can pass values of $getProductType object as mentioned below.

if ($product['product_type'] != null) {
   $getProductType = $this->ProductTypes->find()
     ->where(['title LIKE' => '%'.$product['product_type'].'%'])
     ->first();
   debug($product['product_type']);   // this is not empty
   debug($getProductType);            // this shows 'id' => 1 in array
   debug($getProductType->id);        // this shows 1
   if (!empty($getProductType)) {
      return $this->saveNewBulkProduct($product, $getProductType->category_id,  $getProductType->subcategory_id, $getProductType->id);
   }
}
Haresh Vidja
  • 8,340
  • 3
  • 25
  • 42
1

you have to declare first $p = new stdClass(); on line 11

Dhaval Purohit
  • 1,270
  • 10
  • 28