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


PHP Product::setAttributes方法代码示例

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


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

示例1: 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


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