-2

I have a problem with my code. I want to clone a string and into this string, there are three data. Each parameter is a different table of the DB. When I run the code, Yii2 say error:

Call to a member function save() on an array

this is in my controller:

public function actionClone($id)
{   
    $clone = Helper::get_clone_offer($id);
    if ($clone->save()) Helper::add_history(null, null, $id, '', 
    'Clone', 'Done', 0, 0, 'Offer', 1, 0, '');
    return $this->redirect(['index']);
}

this is in my Helper:

public static function get_clone_offer($id)
{

    $offer = Offers::findOne($id);
    $product = Helper::get_product_name($offer->id);
    $accessory = Helper::get_offer_product_accessories($offer->id);
    $clone = [$offer,$product,$accessory];
    $clone[0]->parent_id = $clone[0]->id; 
    $clone[0]->id += 1000 ;
    return $clone;
}

I'm new in Yii2 and php. Thank you for help.

Muhammad Omer Aslam
  • 22,976
  • 9
  • 42
  • 68
niksolaz
  • 102
  • 10

1 Answers1

0

You havent explained what you are actually trying to achieve, but for the error that is throwing is a definite thing as the $clone you are returning from the get_clone_offer() function is not expected to have a save() method as it is an array.

You should try calling $clone[0]->save() if you are trying to save the $offer model what I can guess you are trying to do because you are setting the Offer model id to the parent_id field in the function get_clone_offer() and $clone is an array of models, and calling

  • $clone[0]->save() will save the Offer model
  • $clone[1]->save() will save the Product model
  • $clone[2]->save() will save the ProductAccessories model

Moreover you should add the methods get_product_name, get_offer_product_accessories in the respective models rather than creating the Helper methods.

Muhammad Omer Aslam
  • 22,976
  • 9
  • 42
  • 68
  • Thanks for the explanation and help, I'm new and I still have not understood well how it works – niksolaz May 24 '18 at 06:35
  • you should go through the documentation to understand it thoroughly it takes time to learn dont worry , the `$clone` is an array of models thats why you should select the model first by using the `index` and then call the `save()` method. hope it helps @niksolaz – Muhammad Omer Aslam May 25 '18 at 08:48