当前位置: 首页>>代码示例>>PHP>>正文


PHP Product::save方法代码示例

本文整理汇总了PHP中app\modules\shop\models\Product::save方法的典型用法代码示例。如果您正苦于以下问题:PHP Product::save方法的具体用法?PHP Product::save怎么用?PHP Product::save使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在app\modules\shop\models\Product的用法示例。


在下文中一共展示了Product::save方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: createProduct

 protected function createProduct($item = [], $createNotExists = true)
 {
     if (empty($item) || !isset($item[static::ELEMENT_ID]) || !isset($item[static::ELEMENT_NAIMENOVANIE])) {
         return false;
     }
     $guid = CommercemlGuid::findOne(['guid' => $item[static::ELEMENT_ID], 'type' => 'PRODUCT']);
     if (empty($guid) && $createNotExists) {
         $category = !empty($item['categories']) ? array_shift($item['categories']) : null;
         $product = new Product();
         $product->name = $product->title = $product->h1 = $item[static::ELEMENT_NAIMENOVANIE];
         $product->slug = Helper::createSlug($product->name);
         $product->main_category_id = isset($this->categoryCache[$category]) ? $this->categoryCache[$category] : $this->rootCategoryCache;
         $product->content = empty($item[static::ELEMENT_OPISANIE]) ? '' : $item[static::ELEMENT_OPISANIE];
         if ($product->validate() && $product->save()) {
             $product->refresh();
             $product->linkToCategory($this->rootCategoryCache);
             $guid = new CommercemlGuid();
             $guid->guid = $item[static::ELEMENT_ID];
             $guid->name = $item[static::ELEMENT_NAIMENOVANIE];
             $guid->model_id = $product->id;
             $guid->type = 'PRODUCT';
             $guid->save();
             return true;
         }
     }
     if (!empty($guid)) {
         /** @var Product $product */
         $product = isset($product) ? $product : $guid->product;
         if (!empty($product)) {
             $product->price = empty($item['price']) ?: array_shift($item['price']);
             $product->content = empty($item[static::ELEMENT_OPISANIE]) ?: $item[static::ELEMENT_OPISANIE];
             $product->name = empty($item[static::ELEMENT_NAIMENOVANIE]) ?: $item[static::ELEMENT_NAIMENOVANIE];
             if (!empty($item['properties'])) {
                 AbstractPropertyEavModel::setTableName($this->objectProduct->eav_table_name);
                 $product_eav = array_reduce(AbstractPropertyEavModel::findByModelId($product->id), function ($result, $item) {
                     $key = $item->property_group_id . ':' . $item->key;
                     $result[$key] = $item;
                     return $result;
                 }, []);
                 $product_groups = array_reduce(ObjectPropertyGroup::getForModel($product), function ($result, $item) {
                     $result[] = $item->property_group_id;
                     return $result;
                 }, []);
                 $product_osv = array_reduce(ObjectStaticValues::findAll(['object_id' => $this->objectProduct->id, 'object_model_id' => $product->id]), function ($result, $item) {
                     $result[] = $item->property_static_value_id;
                     return $result;
                 }, []);
                 foreach ($item['properties'] as $key => $value) {
                     if (isset(static::$propertiesCache[$key])) {
                         /** @var Property $prop */
                         $prop = static::$propertiesCache[$key];
                         if (!in_array($prop->property_group_id, $product_groups)) {
                             $objectGroup = new ObjectPropertyGroup();
                             $objectGroup->object_id = $this->objectProduct->id;
                             $objectGroup->object_model_id = $product->id;
                             $objectGroup->property_group_id = $prop->property_group_id;
                             $objectGroup->save();
                         }
                         if ($prop->has_static_values) {
                             $psv = PropertyStaticValues::findOne(['value' => $value]);
                             if (empty($psv)) {
                                 $psv = new PropertyStaticValues();
                                 $psv->name = $psv->value = $value;
                                 $psv->property_id = $prop->id;
                                 $psv->slug = Helper::createSlug($value);
                                 if ($psv->validate() && $psv->save()) {
                                     $psv->refresh();
                                 } else {
                                     $psv = null;
                                 }
                             }
                             if (!empty($psv) && !in_array($psv->id, $product_osv)) {
                                 $osv = new ObjectStaticValues();
                                 $osv->object_id = $this->objectProduct->id;
                                 $osv->object_model_id = $product->id;
                                 $osv->property_static_value_id = $psv->id;
                                 $osv->save();
                             }
                         } elseif ($prop->is_eav) {
                             $_key = $prop->property_group_id . ':' . $prop->key;
                             if (isset($product_eav[$_key])) {
                                 /** @var AbstractPropertyEavModel $eav */
                                 $eav = $product_eav[$_key];
                                 $eav->value = $value;
                                 $eav->save();
                             } else {
                                 $eav = new AbstractPropertyEavModel();
                                 $eav->object_model_id = $product->id;
                                 $eav->property_group_id = $prop->property_group_id;
                                 $eav->key = $prop->key;
                                 $eav->value = $value;
                                 $eav->save();
                             }
                         }
                     }
                 }
             }
             $product->save();
             return true;
         }
//.........这里部分代码省略.........
开发者ID:tqsq2005,项目名称:dotplant2,代码行数:101,代码来源:XmlFileReader.php

示例2: actionClone

 /**
  * Clone product action.
  * @param integer $id
  * @throws \yii\web\NotFoundHttpException
  */
 public function actionClone($id, $returnUrl = ['index'])
 {
     /** @var Product|HasProperties $model */
     $model = Product::findOne($id);
     if ($model === null) {
         throw new NotFoundHttpException();
     }
     /** @var Product|HasProperties $newModel */
     $newModel = new Product();
     $newModel->setAttributes($model->attributes, false);
     $time = time();
     $newModel->name .= ' (copy ' . date('Y-m-d h:i:s', $time) . ')';
     $newModel->slug .= '-copy-' . date('Ymdhis', $time);
     $newModel->id = null;
     if ($newModel->validate() === false) {
         $newModel->slug = substr(uniqid() . "-" . $model->slug, 0, 80);
     }
     if ($newModel->save()) {
         $object = Object::getForClass(get_class($newModel));
         // save categories
         $categoriesTableName = Object::getForClass(Product::className())->categories_table_name;
         $query = new Query();
         $params = $query->select(['category_id', 'sort_order'])->from($categoriesTableName)->where(['object_model_id' => $model->id])->all();
         if (!empty($params)) {
             $rows = [];
             foreach ($params as $param) {
                 $rows[] = [$param['category_id'], $newModel->id, $param['sort_order']];
             }
             Yii::$app->db->createCommand()->batchInsert($categoriesTableName, ['category_id', 'object_model_id', 'sort_order'], $rows)->execute();
         }
         // save images bindings
         $params = $query->select(['object_id', 'filename', 'image_title', 'image_alt', 'sort_order'])->from(Image::tableName())->where(['object_id' => $object->id, 'object_model_id' => $model->id])->all();
         if (!empty($params)) {
             $rows = [];
             foreach ($params as $param) {
                 $rows[] = [$param['object_id'], $newModel->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();
         }
         $newModelProps = [];
         foreach (array_keys($model->propertyGroups) as $key) {
             $opg = new ObjectPropertyGroup();
             $opg->attributes = ['object_id' => $object->id, 'object_model_id' => $newModel->id, 'property_group_id' => $key];
             $opg->save();
             $props = Property::getForGroupId($key);
             foreach ($props as $prop) {
                 $propValues = $model->getPropertyValuesByPropertyId($prop->id);
                 if ($propValues !== null) {
                     foreach ($propValues->values as $val) {
                         $valueToSave = ArrayHelper::getValue($val, 'psv_id', $val['value']);
                         $newModelProps[$prop->key][] = $valueToSave;
                     }
                 }
             }
         }
         $newModel->saveProperties(['Properties_Product_' . $newModel->id => $newModelProps]);
         Yii::$app->session->setFlash('success', Yii::t('app', 'Product has been cloned successfully.'));
         $this->redirect(['edit', 'id' => $newModel->id, 'returnUrl' => $returnUrl]);
     }
 }
开发者ID:pramana08,项目名称:dotplant2,代码行数:65,代码来源:BackendProductController.php

示例3: up


//.........这里部分代码省略.........
            echo "INFO: Using admin user details provided by ENV variables...\n";
            $username = getenv("ADMIN_USERNAME");
            $email = getenv("ADMIN_EMAIL");
            $password = getenv("ADMIN_PASSWORD");
        } else {
            $stdIn = fopen("php://stdin", "r");
            do {
                echo 'Enter admin username (3 or more chars): ';
                $username = trim(fgets($stdIn));
            } while (mb_strlen($username) < 3);
            do {
                echo 'Enter admin email: ';
                $email = trim(fgets($stdIn));
            } while (preg_match('#^\\w[\\w\\d\\.\\-_]*@[\\w\\d\\.\\-_]+\\.\\w{2,6}$#i', $email) != 1);
            do {
                do {
                    echo 'Enter admin password (8 or more chars): ';
                    $password = trim(fgets($stdIn));
                } while (mb_strlen($password) < 8);
                do {
                    echo 'Confirm admin password: ';
                    $confirmPassword = trim(fgets($stdIn));
                } while (mb_strlen($confirmPassword) < 8);
                if ($password != $confirmPassword) {
                    echo "Password does not match the confirm password\n";
                }
            } while ($password != $confirmPassword);
            fclose($stdIn);
        }
        $user = new User(['scenario' => 'signup']);
        $user->username = $username;
        $user->password = $password;
        $user->email = $email;
        $user->save(false);
        if (getenv("INSTALL_RUSSIAN_TRANSLATIONS")) {
            echo "INFO: Using translations details provided by ENV variables...\n";
            if (trim(strtolower(getenv("INSTALL_RUSSIAN_TRANSLATIONS"))) === 'y') {
                Yii::$app->language = 'ru-RU';
            }
        } else {
            $f = fopen('php://stdin', 'r');
            echo "Install Russian translations? [y/N] ";
            while (true) {
                $answer = trim(fgets($f));
                if ($answer === 'y' || $answer === 'Y') {
                    Yii::$app->language = 'ru-RU';
                    break;
                } elseif ($answer === 'n' || $answer === 'N') {
                    break;
                }
                echo "Install Russian translations? [y/N]";
            }
            fclose($f);
        }
        $this->batchInsert('{{%auth_item}}', ['name', 'type', 'description'], [['admin', '1', Yii::t('app', 'Administrator')], ['manager', '1', Yii::t('app', 'Manager')], ['administrate', '2', Yii::t('app', 'Administrate panel')], ['api manage', '2', Yii::t('app', 'API management')], ['seo manage', '2', Yii::t('app', 'SEO management')], ['task manage', '2', Yii::t('app', 'Task management')], ['user manage', '2', Yii::t('app', 'User management')], ['cache manage', '2', Yii::t('app', 'Cache management')], ['content manage', '2', Yii::t('app', 'Content management')], ['shop manage', '2', Yii::t('app', 'Shop management')], ['order manage', '2', Yii::t('app', 'Order management')], ['category manage', '2', Yii::t('app', 'Category management')], ['product manage', '2', Yii::t('app', 'Product management')], ['property manage', '2', Yii::t('app', 'Property management')], ['view manage', '2', Yii::t('app', 'View management')], ['review manage', '2', Yii::t('app', 'Review management')], ['navigation manage', '2', Yii::t('app', 'Navigation management')], ['form manage', '2', Yii::t('app', 'Form management')], ['media manage', '2', Yii::t('app', 'Media management')], ['order status manage', '2', Yii::t('app', 'Order status management')], ['payment manage', '2', Yii::t('app', 'Payment type management')], ['shipping manage', '2', Yii::t('app', 'Shipping option management')], ['newsletter manage', '2', Yii::t('app', 'Newsletter management')], ['monitoring manage', '2', Yii::t('app', 'Monitoring management')], ['data manage', '2', Yii::t('app', 'Data management')], ['setting manage', '2', Yii::t('app', 'Setting management')]]);
        $this->batchInsert('{{%auth_item_child}}', ['parent', 'child'], [['shop manage', 'category manage'], ['shop manage', 'product manage'], ['shop manage', 'order manage'], ['manager', 'administrate'], ['manager', 'content manage'], ['manager', 'order manage'], ['manager', 'shop manage'], ['manager', 'category manage'], ['manager', 'product manage'], ['manager', 'property manage'], ['manager', 'view manage'], ['manager', 'review manage'], ['manager', 'navigation manage'], ['manager', 'form manage'], ['manager', 'media manage'], ['admin', 'administrate'], ['admin', 'api manage'], ['admin', 'order manage'], ['admin', 'seo manage'], ['admin', 'task manage'], ['admin', 'user manage'], ['admin', 'cache manage'], ['admin', 'content manage'], ['admin', 'shop manage'], ['admin', 'category manage'], ['admin', 'product manage'], ['admin', 'property manage'], ['admin', 'view manage'], ['admin', 'review manage'], ['admin', 'navigation manage'], ['admin', 'form manage'], ['admin', 'media manage'], ['admin', 'order status manage'], ['admin', 'payment manage'], ['admin', 'shipping manage'], ['admin', 'monitoring manage'], ['admin', 'newsletter manage'], ['admin', 'data manage'], ['admin', 'setting manage']]);
        $this->insert('{{%auth_assignment}}', ['item_name' => 'admin', 'user_id' => $user->id]);
        // demo data
        $demo = null;
        if (getenv("INSTALL_DEMO_DATA")) {
            $demo = getenv("INSTALL_DEMO_DATA");
        } else {
            $stdIn = fopen("php://stdin", "r");
            do {
                echo 'Do you want to install demo data [y/n]: ';
                $demo = strtolower(trim(fgets($stdIn)));
开发者ID:tqsq2005,项目名称:dotplant2,代码行数:67,代码来源:m141023_084857_init.php


注:本文中的app\modules\shop\models\Product::save方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。