/**
  * @param $id
  * @throws NotFoundHttpException
  * @throws \yii\db\Exception
  */
 public function actionGenerate($id)
 {
     $post = \Yii::$app->request->post();
     if (!isset($post['GeneratePropertyValue'])) {
         throw new NotFoundHttpException();
     }
     $parent = Product::findById($id, null);
     if ($parent === null) {
         throw new NotFoundHttpException();
     }
     $object = Object::getForClass(Product::className());
     $catIds = (new Query())->select('category_id')->from([$object->categories_table_name])->where('object_model_id = :id', [':id' => $id])->orderBy(['sort_order' => SORT_ASC, 'id' => SORT_ASC])->column();
     if (isset($post['GeneratePropertyValue'])) {
         $generateValues = $post['GeneratePropertyValue'];
         $post[HasProperties::FIELD_ADD_PROPERTY_GROUP]['Product'] = $post['PropertyGroup']['id'];
     } else {
         $generateValues = [];
     }
     $parent->option_generate = Json::encode(['group' => $post['PropertyGroup']['id'], 'values' => $generateValues]);
     $parent->save();
     $postProperty = [];
     foreach ($post['GeneratePropertyValue'] as $key_property => $values) {
         $inner = [];
         foreach ($values as $key_value => $trash) {
             $inner[] = [$key_property => $key_value];
         }
         $postProperty[] = $inner;
     }
     $optionProperty = self::generateOptions($postProperty);
     foreach ($optionProperty as $option) {
         /** @var Product|HasProperties $model */
         $model = new Product();
         $model->load($post);
         $model->parent_id = $parent->id;
         $nameAppend = [];
         $slugAppend = [];
         $tempPost = [];
         // @todo something
         foreach ($option as $optionValue) {
             foreach ($optionValue as $propertyKey => $propertyValue) {
                 if (!isset($valueModels[$propertyKey])) {
                     $propertyStaticValues = PropertyStaticValues::findOne($propertyValue);
                     $propertyValue = PropertyStaticValues::findById($propertyValue);
                     $key = $propertyStaticValues->property->key;
                     $tempPost[$key] = $propertyValue;
                 }
                 $nameAppend[] = $propertyValue['name'];
                 $slugAppend[] = $propertyValue['id'];
             }
         }
         $model->measure_id = $parent->measure_id;
         $model->name = $parent->name . ' (' . implode(', ', $nameAppend) . ')';
         $model->slug = $parent->slug . '-' . implode('-', $slugAppend);
         $save_model = $model->save();
         $postPropertyKey = 'Properties_Product_' . $model->id;
         $post[$postPropertyKey] = $tempPost;
         if ($save_model) {
             foreach (array_keys($parent->propertyGroups) as $key) {
                 $opg = new ObjectPropertyGroup();
                 $opg->attributes = ['object_id' => $parent->object->id, 'object_model_id' => $model->id, 'property_group_id' => $key];
                 $opg->save();
             }
             $model->saveProperties(['Properties_Product_' . $model->id => $parent->abstractModel->attributes]);
             $model->saveProperties($post);
             unset($post[$postPropertyKey]);
             $add = [];
             foreach ($catIds as $value) {
                 $add[] = [$value, $model->id];
             }
             if (!empty($add)) {
                 Yii::$app->db->createCommand()->batchInsert($object->categories_table_name, ['category_id', 'object_model_id'], $add)->execute();
             }
             $params = $parent->images;
             if (!empty($params)) {
                 $rows = [];
                 foreach ($params as $param) {
                     $rows[] = [$param['object_id'], $model->id, $param['filename'], $param['image_title'], $param['image_alt'], $param['sort_order']];
                 }
                 Yii::$app->db->createCommand()->batchInsert(Image::tableName(), ['object_id', 'object_model_id', 'filename', 'image_title', 'image_alt', 'sort_order'], $rows)->execute();
             }
         }
     }
 }