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"]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -10,24 +10,22 @@ use app\models\Area;
|
||||
/**
|
||||
* AreaSearch represents the model behind the search form of `app\models\Area`.
|
||||
*/
|
||||
class AreaSearch extends Area
|
||||
{
|
||||
class AreaSearch extends Area {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
public function rules() {
|
||||
return [
|
||||
[['id', 'code', 'pid', 'created_at', 'modified_at'], 'integer'],
|
||||
[['name', 'description'], 'safe'],
|
||||
[['id', 'code', 'pid'], 'integer'],
|
||||
[['name', 'description'], 'safe'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function scenarios()
|
||||
{
|
||||
public function scenarios() {
|
||||
// bypass scenarios() implementation in the parent class
|
||||
return Model::scenarios();
|
||||
}
|
||||
@@ -39,8 +37,7 @@ class AreaSearch extends Area
|
||||
*
|
||||
* @return ActiveDataProvider
|
||||
*/
|
||||
public function search($params)
|
||||
{
|
||||
public function search($params) {
|
||||
$query = Area::find();
|
||||
|
||||
// add conditions that should always apply here
|
||||
@@ -67,8 +64,9 @@ class AreaSearch extends Area
|
||||
]);
|
||||
|
||||
$query->andFilterWhere(['like', 'name', $this->name])
|
||||
->andFilterWhere(['like', 'description', $this->description]);
|
||||
->andFilterWhere(['like', 'description', $this->description]);
|
||||
|
||||
return $dataProvider;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user