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
   ....
)
2

2 Answers 2

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);
   }
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.