106 lines
2.6 KiB
PHP
106 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace app\models;
|
|
|
|
use Yii;
|
|
|
|
/**
|
|
* This is the model class for table "area".
|
|
*
|
|
* @property int $id
|
|
* @property int $code
|
|
* @property int $pid
|
|
* @property string $name
|
|
* @property string $description
|
|
* @property int $created_at
|
|
* @property int $modified_at
|
|
*/
|
|
class Area extends \yii\db\ActiveRecord {
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public static function tableName() {
|
|
return 'area';
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function rules() {
|
|
return [
|
|
[['code', 'name'], 'required'],
|
|
[['code', 'pid', 'created_at', 'modified_at'], 'integer'],
|
|
[['description'], 'string'],
|
|
[['name'], 'string', 'max' => 100],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function attributeLabels() {
|
|
return [
|
|
'id' => 'ID',
|
|
'code' => 'Mã khu vực',
|
|
'pid' => 'Khu vực cha',
|
|
'name' => 'Tên khu vực',
|
|
'description' => 'Chú thích',
|
|
'created_at' => 'Thời gian tạo',
|
|
'modified_at' => 'Thời gian sửa',
|
|
];
|
|
}
|
|
|
|
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"]);
|
|
}
|
|
}
|
|
|
|
}
|