/** * The default implementation returns the names of the columns whose values have been populated into Product. */ public function fields() { return array_merge(parent::fields(), ['sku', 'name', 'pictures', 'category' => function () { $categorys = $this->category; if (!empty($categorys['id'])) { $categorys['id'] = $categorys['id'] . ''; } return $categorys; }, 'intro' => function () { return ProductInfo::findByPk($this->_id)['intro']; }, 'isAssigned' => function () { if ($this->isBindCode) { return true; } else { return false; } }, 'codeNum' => function () { return PromotionCode::count(['productId' => $this->_id]); }, 'isSelected' => function () { $goods = Goods::findOne(['productId' => $this->_id]); if (empty($goods)) { return false; } else { return true; } }]); }
public function actionUpdate($id) { $params = $this->getParams(); $accountId = $this->getAccountId(); //check the product name $id = new MongoId($id); $where = ["_id" => $id]; $product = Product::findOne($where); if (empty($product)) { throw new InvalidParameterException(Yii::t('product', 'product_deleted')); } $oldName = $product['name']; $oldSku = $product['sku']; $oldCategoryId = isset($product['category']['id']) ? $product['category']['id'] . '' : ''; unset($params['sku']); //cant not update the sku //check the property whether to be required Product::checkParam($params, $accountId); $intro = isset($params['intro']) ? $params['intro'] : " "; unset($params['intro']); $product->load($params, ''); if ($product->save()) { //update goods and storeGoods $productCategory = $product->category; if ($oldName !== $product->name || (string) $productCategory['id'] != $oldCategoryId) { $this->attachBehavior('ProductBehavior', new ProductBehavior()); $this->update($product->_id, $product->name, $productCategory['id']); } //update the intro $productInfo = ProductInfo::findByPk($product->_id); $productInfo->intro = $intro; $productInfo->save(); return $product; } else { throw new ServerErrorHttpException('Fail to update product'); } }