init Area full CRUD
This commit is contained in:
@@ -51,4 +51,55 @@ class Area extends \yii\db\ActiveRecord {
|
||||
];
|
||||
}
|
||||
|
||||
public static function areaArray() {
|
||||
$lists = self::find()->all();
|
||||
$results = [];
|
||||
foreach ($lists as $key => $value) {
|
||||
$results[$value->code] = $value->name;
|
||||
}
|
||||
return $results;
|
||||
}
|
||||
|
||||
public static function areaArrayWithOut($id) {
|
||||
$lists = self::find()->andWhere(["<>", "id", $id])->all();
|
||||
$results = [];
|
||||
foreach ($lists as $key => $value) {
|
||||
$results[$value->code] = $value->name;
|
||||
}
|
||||
return $results;
|
||||
}
|
||||
|
||||
public function create($data) {
|
||||
$r = $this->load([
|
||||
"code" => $data["Code"],
|
||||
"pid" => $data["Pid"],
|
||||
"name" => $data["Name"],
|
||||
"description" => $data["Description"],
|
||||
"created_at" => time(),
|
||||
"modified_at" => time()
|
||||
], '');
|
||||
if ($r) {
|
||||
try {
|
||||
$this->save();
|
||||
return $this->id;
|
||||
} catch (\Exception $ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static function deleteArea($id) {
|
||||
if ($id == 1)
|
||||
return;
|
||||
$model = self::findOne($id);
|
||||
if ($model) {
|
||||
$childs = self::find()->andWhere(["pid" => $model->code])->all();
|
||||
foreach ($childs as $key => $value) {
|
||||
self::deleteArea($value->id);
|
||||
}
|
||||
$model->delete();
|
||||
common::insertSystemLogs(["action" => "delete", "description" => "Xóa khu vực: " . $model->name, "type" => "area"]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user