模型定义拼音自动完成字段

pull/319/head
Henry 2021-06-09 14:03:12 +08:00
parent 119b0273d4
commit a5eb7ef3b7
1 changed files with 20 additions and 0 deletions

View File

@ -13,9 +13,16 @@ class AuthRule extends Model
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
// 数据自动完成字段
protected $insert = ['py', 'pinyin'];
protected $update = ['py', 'pinyin'];
// 拼音对象
protected static $pinyin = null;
protected static function init()
{
self::$pinyin = new \Overtrue\Pinyin\Pinyin('Overtrue\Pinyin\MemoryFileDictLoader');
self::beforeWrite(function ($row) {
if (isset($_POST['row']) && is_array($_POST['row']) && isset($_POST['row']['condition'])) {
$originRow = $_POST['row'];
@ -37,4 +44,17 @@ class AuthRule extends Model
return ['addtabs' => __('Addtabs'), 'dialog' => __('Dialog'), 'ajax' => __('Ajax'), 'blank' => __('Blank')];
}
public function setPyAttr($value, $data) {
if (isset($data['title']) && $data['title']) {
return self::$pinyin->abbr($data['title']);
}
return '';
}
public function setPinyinAttr($value, $data) {
if (isset($data['title']) && $data['title']) {
return self::$pinyin->permalink($data['title'], '');
}
return '';
}
}