public function actionCreate() { $params = $this->getParams(); $accountId = $this->getAccountId(); $categoryId = $params['categoryId']; $productcategory = ProductCategory::findByPk(new MongoId($categoryId)); if (count($productcategory->properties) >= 20) { throw new BadRequestHttpException('20 properties at most'); } if (empty($productcategory)) { throw new BadRequestHttpException('categoryId_invaild'); } //check the propertyId if (empty($params['categoryPropertyId'])) { throw new BadRequestHttpException('missing params categoryPropertyId'); } else { if (!preg_match(MemberProperty::PROPERTY_ID_PATTERN, $params['categoryPropertyId'])) { throw new InvalidParameterException(['categoryPropertyId' => Yii::t('product', 'propertyId_format_error')]); } $result = CategoryProperty::checkUniqueName('categoryPropertyId', $params['categoryPropertyId'], $categoryId, $accountId); if (false === $result) { throw new InvalidParameterException(['categoryPropertyId' => Yii::t("product", "propertyId_isUsed")]); } } if (!isset($params['name'])) { throw new InvalidParameterException(['categoryPropertyName' => Yii::t("product", "property_required")]); } $result = CategoryProperty::checkUniqueName('property', $params['name'], $categoryId, $accountId, ''); if (false === $result) { throw new InvalidParameterException(['categoryPropertyName' => Yii::t("product", "propertyName_isUsed")]); } //add product properties $defaultValue = !isset($params['defaultValue']) ? '' : $params['defaultValue']; $type = !isset($params['type']) ? 'input' : $params['type']; $propertyName = $params['name']; $params = $productcategory->formatPropertiesStr($params); $uuid = $params['uuid']; unset($params['uuid']); $productcategory->load($params, ''); if ($productcategory->save()) { //update the product category $addproperties = ['id' => $uuid, 'name' => $propertyName, 'value' => $defaultValue, 'type' => $type]; // Product::updateAll(['$push' => ['category.properties' => $addproperties]], ['accountId' => $accountId]); CategoryProperty::updateProductProperty($addproperties, $productcategory->_id, 'create'); foreach ($productcategory->properties as $property) { if ($property['id'] == $uuid) { return $property; } } } else { throw new ServerErrorHttpException('Fail to create property'); } }
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); }
/** * The default implementation returns the names of the columns whose values have been populated into storeGoods. */ public function fields() { return array_merge(parent::fields(), ['storeId' => function () { return (string) $this->storeId; }, 'productId' => function () { return (string) $this->productId; }, 'pictures', 'status', 'onSaleTime' => function () { return empty($this->onSaleTime) ? '' : MongodbUtil::MongoDate2String($this->onSaleTime, 'Y-m-d H:i'); }, 'offShelfTime' => function () { return empty($this->offShelfTime) || !empty($this->onSaleTime) ? '' : MongodbUtil::MongoDate2String($this->offShelfTime, 'Y-m-d H:i'); }, 'categoryName' => function () { $category = ProductCategory::findByPk($this->categoryId); if ($category) { return $category['name']; } else { return ''; } }, 'productName', 'sku', 'price' => function () { return self::formatPrice($this->price); }, 'createdAt' => function () { return empty($this->createdAt) ? '' : MongodbUtil::MongoDate2String($this->createdAt, 'Y-m-d H:i'); }]); }
/** * @param $id string the category id * @param $propertyId string the property id * */ public static function removeProperty($id, $propertyId) { ProductCategory::updateAll(['$pull' => ['properties' => ['id' => $propertyId]]], ['_id' => new \MongoId($id)]); return ProductCategory::findByPk(new MongoId($id)); }
/** * The default implementation returns the names of the columns whose values have been populated into Goods. */ public function fields() { return array_merge(parent::fields(), ['productId' => function () { return $this->productId . ''; }, 'pictures', 'score', 'total', 'usedCount', 'status', 'onSaleTime' => function () { if ($this->onSaleTime) { return MongodbUtil::MongoDate2String($this->onSaleTime, 'Y-m-d H:i'); } else { return ''; } }, 'offShelfTime' => function () { return empty($this->offShelfTime) || !empty($this->onSaleTime) ? '' : MongodbUtil::MongoDate2String($this->offShelfTime, 'Y-m-d H:i'); }, 'url', 'order', 'categoryName' => function () { $category = ProductCategory::findByPk($this->categoryId); if ($category) { return $category['name']; } else { return ''; } }, 'productName', 'sku', 'clicks', 'description', 'receiveModes', 'addresses' => function () { $addresses = []; if (!empty($this->addresses)) { foreach ($this->addresses as $address) { $addresses[] = (string) $address; } } return $addresses; }, 'createdAt' => function () { return MongodbUtil::MongoDate2String($this->createdAt, 'Y-m-d H:i'); }]); }