當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。