public function actionUpdate($id) { $params = $this->getParams(); $accountId = $this->getAccountId(); if (empty($params['name'])) { throw new BadRequestHttpException("category required"); } $productCategoryInfo = ProductCategory::findByPk($id); if (ProductCategory::RESERVATION_CATEGORY_NAME == $productCategoryInfo->name) { throw new InvalidParameterException(Yii::t('product', 'can_not_update')); } if (ProductCategory::RESERVATION_CATEGORY_NAME == $params['name']) { throw new InvalidParameterException(Yii::t('product', 'categoryName_isUsed')); } $result = CategoryProperty::checkUniqueName('category', $params['name'], $id, $accountId, ''); if (false === $result) { throw new InvalidParameterException([$id => Yii::t("product", "categoryName_isUsed")]); } $where = ['_id' => new \MongoId($id), 'accountId' => $accountId, 'isDeleted' => false]; ProductCategory::updateAll(['name' => $params['name']], $where); Product::updateAll(['category.name' => $params['name']], ['category.id' => new \MongoId($id)]); return ProductCategory::findByPk($id); }
/** * update the property * @param $id,int,proprty id * @param $params,array * @param $accountId,MongoId */ public static function updateProperty($id, $params, $accountId) { //update category property $where = ['accountId' => $accountId, '_id' => new \MongoId($id), 'properties.id' => $params['propertyId']]; $categoryPropertyInfo = ProductCategory::findOne($where); if ($categoryPropertyInfo) { //only update the name and isrequired $addproperties = []; if (!empty($categoryPropertyInfo->properties)) { $where = ['properties.id' => $params['propertyId']]; ProductCategory::updateAll(['$set' => ['properties.$.name' => $params['name'], 'properties.$.isRequired' => $params['isRequired']]], $where); } //check whether have properties $productInfo = Product::findOne(['category.id' => new \MongoId($id)]); if ($productInfo) { $where = ['category.properties.id' => $params['propertyId']]; $updateData = ['category.properties.$.name' => $params['name'], 'category.properties.$.value' => $params['defaultValue']]; Product::updateAll(['$set' => $updateData], $where); unset($productInfo); } unset($where); return $categoryPropertyInfo; } else { throw new BadRequestHttpException("categoryId or propertyId invalid"); } }