/** * 保存模板 */ public function actionSave() { $type = Yii::$app->request->post("dataType", 0); $data = Yii::$app->request->post("data"); $result = ["status" => 0, 'msg' => '']; $transaction = Yii::$app->db->beginTransaction(); try { ViewTemplate::deleteAll(["type" => $type]); $data = json_decode($data, true); $data = array_filter($data); foreach ($data as $k => $item) { $model = new ViewTemplate(); $model->id = $k; $model->attributes = json_encode($item["attributes"]); $model->links = isset($item["links"]) ? json_encode($item["links"]) : null; $model->type = $type; $model->device_id = $item["data"]["id"]; $model->areaId = $model->getAreaId($item["attributes"]["cx"], $item["attributes"]["cy"]); if (!$model->save()) { $transaction->rollback(); throw new Exception("item error:" . $item["data"]["label"] . print_r($model->getErrors(), true)); } } $transaction->commit(); $result["status"] = 1; } catch (Exception $e) { $transaction->rollback(); $result["msg"] = $e->getMessage(); } return Json::encode($result); }