AdminRole.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace backend\models;
  3. use Yii;
  4. /**
  5. *
  6. * This is the model class for table "{{%admin_role}}".
  7. *
  8. * @property int $id
  9. * @property int $role_id 角色id
  10. * @property int $admin_id 账号id
  11. * @property int|null $created_at 添加时间
  12. * @property int|null $updated_at 修改时间
  13. */
  14. class AdminRole extends \common\models\base\BaseActiveRecord
  15. {
  16. /**
  17. * {@inheritdoc}
  18. */
  19. public static function tableName()
  20. {
  21. return '{{%admin_role}}';
  22. }
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function rules()
  27. {
  28. return [
  29. [['role_id', 'admin_id', 'created_at', 'updated_at'], 'integer'],
  30. ];
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function attributeLabels()
  36. {
  37. return [
  38. 'id' => 'ID',
  39. 'role_id' => 'Role ID',
  40. 'admin_id' => 'Admin ID',
  41. 'created_at' => 'Created At',
  42. 'updated_at' => 'Updated At',
  43. ];
  44. }
  45. public static function setData(array $post){
  46. try{
  47. if(!empty($post['id'])){
  48. $model = self::findOne(['id' => $post['id']]);
  49. if(empty($model)) throw new \Exception('服务不存在或已删除');
  50. }else{
  51. $model = new self();
  52. $model->loadDefaultValues();
  53. }
  54. isset($post['role_id']) && $model->role_id = $post['role_id'];
  55. isset($post['admin_id']) && $model->admin_id = $post['admin_id'];
  56. $res = $model->save();
  57. if($res === false) throw new \Exception($model->getErrorMessage());
  58. return $model;
  59. }catch(\Exception $e){
  60. self::$static_error = $e->getMessage();
  61. return false;
  62. }
  63. }
  64. }