public function del($id) { $connection = Yii::$app->db; //开启事务 $transaction = $connection->beginTransaction(); try { GoodsInventory::deleteAll(['colorid' => $id]); GoodsColor::findOne($id)->delete(); //提交 $transaction->commit(); return true; } catch (Exception $e) { $transaction->rollBack(); return false; } }
/** * 根据颜色和尺寸获取不同库存 */ public function actionInventory() { $inventoryNum = 0; $request = Yii::$app->getRequest(); $goodsId = $request->post('id'); $size = $request->post('size'); $colorId = $request->post('dataId'); if ($request->getIsPost() && $request->getIsAjax()) { if ($goodsId) { //组装查询条件 $conditionsInventory = ['gid' => $goodsId]; //如果颜色id是否为空,或尺寸为空,组装查询条件 if (!$colorId) { $conditionsInventory['size'] = $size; } else { if (!$size) { $conditionsInventory['colorid'] = $colorId; } else { $conditionsInventory['size'] = $size; $conditionsInventory['colorid'] = $colorId; } } //查询库存 $inventorys = GoodsInventory::findAll($conditionsInventory); if (empty($inventorys)) { $this->ajaxReturn(false, [], '库存不足,请刷新重试'); } foreach ($inventorys as $key => $inventory) { $inventoryNum += $inventory['inventory']; } $this->ajaxReturn(true, $inventoryNum, '成功'); } $this->ajaxReturn(false, [], '请刷新重试'); } $this->ajaxReturn(false, [], '非法请求,请刷新重试'); }