/** *check the property is required */ public static function checkParam($params, $accountId) { //check the sku if (isset($params['sku'])) { $result = Product::find()->where(['sku' => $params['sku'], 'isDeleted' => false])->one(); if ($result) { throw new InvalidParameterException(['number' => Yii::t("product", "number_isUsed")]); } } if (empty($params['category'])) { return true; } $where = ['isDeleted' => false, 'accountId' => $accountId, '_id' => new \MongoId($params['category']['id'])]; $categoryInfos = ProductCategory::find()->where($where)->all(); if (empty($categoryInfos)) { throw new ServerErrorHttpException('Fail to find the properties'); } $requiredData = []; foreach ($categoryInfos as $categoryInfo) { foreach ($categoryInfo['properties'] as $cproperties) { if ($cproperties['isRequired']) { $requiredData[] = $cproperties['id']; } } } if (count($requiredData) > 0 && empty($params['category']['properties'])) { throw new InvalidParameterException([$requiredData[0] => Yii::t('product', 'property_required')]); } else { if (count($requiredData) > 0 && !empty($params['category']['properties'])) { foreach ($params['category']['properties'] as $properties) { $userData[] = $properties['id']; } } } foreach ($requiredData as $key => $value) { if (!in_array($value, $userData)) { throw new InvalidParameterException([$value => \Yii::t('product', 'property_required')]); } } }
/** * search for the message * @param $params,array * @param $accountId,mongoId */ public static function search($params, $accountId) { $condition = ['accountId' => $accountId, 'isDeleted' => \backend\components\BaseModel::NOT_DELETED]; //$orderBy = ['createdAt' => SORT_DESC]; $orderBy = self::normalizeOrderBy($params); $productCategoryInfos = ProductCategory::find()->where($condition)->orderBy($orderBy)->all(); if (!empty($productCategoryInfos)) { foreach ($productCategoryInfos as $k => $productCategoryInfo) { $productCategoryInfos[$k]['properties'] = CategoryProperty::showOrderPeoperty($productCategoryInfo->properties); } } return ['items' => $productCategoryInfos]; }