You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
simple-yewu/common/models/DepartmentT.php

96 lines
2.3 KiB

5 years ago
<?php
namespace common\models;
use Yii;
/**
* This is the model class for table "department_t".
*
* @property integer $id
* @property string $name
* @property integer $company_id
* @property integer $parent_id
* @property string $created_at
* @property string $updated_at
*/
class DepartmentT extends \common\models\Base
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'department_t';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['name'], 'required'],
[['company_id', 'parent_id'], 'integer'],
[['created_at', 'updated_at'], 'safe'],
[['name'], 'string', 'max' => 50],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'name' => 'Name',
'company_id' => 'Company ID',
'parent_id' => 'Parent ID',
'created_at' => 'Created At',
'updated_at' => 'Updated At',
];
}
public function getDepartments()
{
return $this->hasMany(DepartmentT::className(),['parent_id'=>'id']);
}
static public function getTree($company_id=0,$parent_id=0,$level=0)
{
$items = DepartmentT::find()
->where('parent_id='.$parent_id.' and company_id='.$company_id)
->all();
$data = array();
foreach($items as $item) {
$tmp_str = '';
if($level > 1)
$tmp_str .= str_repeat('  ',$level-1);
if($level > 0)
$tmp_str .= str_repeat('  ',1);
$tmp_str .= $item->name;
$item->name = $tmp_str;
$data[] = $item;
$tmp_data = array_merge($data,DepartmentT::getTree($company_id,$item->id,$level+1));
$data = $tmp_data;
}
return $data;
}
public function hasChild($id)
{
$items = DepartmentT::getTree($this->company_id,$this->id);
foreach($items as $item) {
if($item->id == $id)
return true;
}
return false;
}
public function getCompany()
{
return $this->hasOne(CompanyT::className(),['id'=>'company_id']);
}
}