Skip to content

Commit

Permalink
添加示例模型类
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyucheng committed May 7, 2020
1 parent b8279f3 commit 81f030b
Show file tree
Hide file tree
Showing 4 changed files with 278 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ $ composer require xihrni/yii2-behavior-log
yii migrate --migrationPath=@vendor/xihrni/yii2-behavior-log/migrations
```

### Model
复制已准备好的模型到项目中,目录为:`@vendor/xihrni/yii2-behavior-log/models`

### Controller
```php
<?php
Expand Down
123 changes: 123 additions & 0 deletions models/Admin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php

namespace app\models;

use Yii;

/**
* 管理员模型
*
* @property int $id
* @property string $username 用户名
* @property string $password_hash 加密密码
* @property string $password_reset_token 重置密码令牌
* @property string $auth_key 认证密钥
* @property string $access_token 访问令牌
* @property string $mobile 手机号码
* @property string $realname 真实姓名
* @property int $is_trash 是否删除,0=>否,1=>是
* @property int $status 状态,0=>禁用,1=>启用
* @property string $created_at 创建时间
* @property string $updated_at 更新时间
* @property string $deleted_at 删除时间
* @property string $last_login_at 最后登录时间
* @property string $last_login_ip 最后登录IP
* @property int $allowance 请求剩余次数
* @property string $allowance_updated_at 请求更新时间
*
* @property AdminBehaviorLog[] $adminBehaviorLogs
*/
class Admin extends BaseActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return '{{%admin}}';
}

/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['id', 'is_trash', 'status'], 'integer', 'min' => 0],

[['username', 'mobile', 'realname'], 'string', 'max' => 16],
[['password_hash'], 'string', 'max' => 255],
[['password_reset_token', 'auth_key', 'access_token'], 'string', 'max' => 64],

[['created_at', 'updated_at', 'deleted_at', 'last_login_at'], 'datetime', 'format' => 'yyyy-MM-dd HH:mm:ss'],

[['last_login_ip'], 'ip'],
[['mobile'], 'match', 'pattern' => '/^1([356789]{1})\d{9}$/'],

[['password_reset_token', 'auth_key', 'access_token', 'mobile'], 'default', 'value' => null],
[['username', 'password_hash', 'realname'], 'default', 'value' => ''],
[['is_trash', 'allowance', 'allowance_updated_at'], 'default', 'value' => 0],
[['status'], 'default', 'value' => 1],

[['username'], 'unique'],
[['access_token'], 'unique'],
[['auth_key'], 'unique'],
[['password_reset_token'], 'unique'],
[['mobile'], 'unique'],
];
}

/**
* {@inheritdoc}
*/
public function beforeSave($insert)
{
$isValid = parent::beforeSave($insert);

if (!$isValid) {
return $isValid;
}

// 更新操作
if (!$insert) {
// 不允许更新的字段
$this->username = $this->oldAttributes['username'];
}

return true;
}

/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => Yii::t('app', 'ID'),
'username' => Yii::t('app', '用户名'),
'password_hash' => Yii::t('app', '加密密码'),
'password_reset_token' => Yii::t('app', '重置密码令牌'),
'auth_key' => Yii::t('app', '认证密钥'),
'access_token' => Yii::t('app', '访问令牌'),
'mobile' => Yii::t('app', '手机号码'),
'realname' => Yii::t('app', '真实姓名'),
'is_trash' => Yii::t('app', '是否删除,0=>否,1=>是'),
'status' => Yii::t('app', '状态,0=>禁用,1=>启用'),
'created_at' => Yii::t('app', '创建时间'),
'updated_at' => Yii::t('app', '更新时间'),
'deleted_at' => Yii::t('app', '删除时间'),
'last_login_at' => Yii::t('app', '最后登录时间'),
'last_login_ip' => Yii::t('app', '最后登录IP'),
'allowance' => Yii::t('app', '请求剩余次数'),
'allowance_updated_at' => Yii::t('app', '请求更新时间'),
];
}

/**
* @return \yii\db\ActiveQuery
*/
public function getAdminBehaviorLogs()
{
return $this->hasMany(AdminBehaviorLog::className(), ['admin_id' => 'id']);
}
}
101 changes: 101 additions & 0 deletions models/AdminBehaviorLog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

namespace app\models;

use Yii;

/**
* 管理员行为日志模型
*
* @property int $id
* @property int $admin_id 管理员ID
* @property string $module 模块
* @property string $controller 控制器
* @property string $action 操作
* @property string $route 路由
* @property string $method 方法
* @property string $headers 请求头(json)
* @property string $params 请求参数(json)
* @property string $body 请求体(json)
* @property string $authorization 身份认证
* @property string $request_ip 请求IP
* @property string $response 响应结果(json)
* @property int $is_trash 是否删除,0=>否,1=>是
* @property int $status 状态,0=>禁用,1=>启用
* @property string $created_at 创建时间
* @property string $updated_at 更新时间
* @property string $deleted_at 删除时间
*
* @property Admin $admin
*/
class AdminBehaviorLog extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return '{{%admin_behavior_log}}';
}

/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['id', 'admin_id', 'is_trash', 'status'], 'integer', 'min' => 0],

[['module'], 'string', 'max' => 64],
[['controller', 'action'], 'string', 'max' => 32],
[['route', 'authorization'], 'string', 'max' => 255],
[['method'], 'string', 'max' => 8],
[['request_ip'], 'string', 'max' => 16],
[['headers', 'params', 'body', 'response'], 'string'],

[['created_at', 'updated_at', 'deleted_at'], 'datetime', 'format' => 'yyyy-MM-dd HH:mm:ss'],

[['admin_id'], 'default', 'value' => null],
[['module', 'action', 'route', 'controller', 'method', 'headers', 'params', 'body', 'authorization', 'request_ip', 'response',], 'default', 'value' => ''],
[['is_trash'], 'default', 'value' => 0],
[['status'], 'default', 'value' => 1],

[['admin_id'], 'exist', 'skipOnError' => true, 'targetClass' => Admin::className(), 'targetAttribute' => ['admin_id' => 'id', 0 => 'is_trash']],
];
}

/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => Yii::t('app', 'ID'),
'admin_id' => Yii::t('app', '管理员ID'),
'module' => Yii::t('app', '模块'),
'controller' => Yii::t('app', '控制器'),
'action' => Yii::t('app', '操作'),
'route' => Yii::t('app', '路由'),
'method' => Yii::t('app', '方法'),
'headers' => Yii::t('app', '请求头(json)'),
'params' => Yii::t('app', '请求参数(json)'),
'body' => Yii::t('app', '请求体(json)'),
'authorization' => Yii::t('app', '身份认证'),
'request_ip' => Yii::t('app', '请求IP'),
'response' => Yii::t('app', '响应结果(json)'),
'is_trash' => Yii::t('app', '是否删除,0=>否,1=>是'),
'status' => Yii::t('app', '状态,0=>禁用,1=>启用'),
'created_at' => Yii::t('app', '创建时间'),
'updated_at' => Yii::t('app', '更新时间'),
'deleted_at' => Yii::t('app', '删除时间'),
];
}

/**
* @return \yii\db\ActiveQuery
*/
public function getAdmin()
{
return $this->hasOne(Admin::className(), ['id' => 'admin_id']);
}
}
51 changes: 51 additions & 0 deletions models/BaseActiveRecord.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace app\models;

use Yii;
use yii\behaviors\AttributeTypecastBehavior;
use xihrni\yii2\behaviors\TimeBehavior;

/**
* 基础活跃记录类
*
* Class BaseActiveRecord
* @package app\models
*/
class BaseActiveRecord extends \yii\db\ActiveRecord
{
/**
* 行为
*
* @return array
*/
public function behaviors()
{
return array_merge(parent::behaviors(), [
'typecast' => [
'class' => AttributeTypecastBehavior::className(),
'typecastAfterValidate' => true,
'typecastBeforeSave' => true,
'typecastAfterFind' => true,
],
'time' => [
'class' => TimeBehavior::className(),
],
]);
}

/**
* 软删除
*
* @return bool
*/
public function softDelete()
{
$this->is_trash = 1;
$this->deleted_at = date('Y-m-d H:i:s');

// TODO 更新唯一索引值

return $this->save(true, ['is_trash', 'deleted_at']);
}
}

0 comments on commit 81f030b

Please sign in to comment.