123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- <?php
- namespace app\admin\model;
- use think\Model;
- /**
- * Test
- */
- class Test extends Model
- {
- // 表名
- protected $name = 'test';
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = true;
- // 追加属性
- protected $append = [
- 'city_text',
- ];
- // 字段类型转换
- protected $type = [
- 'array' => 'json',
- 'timestamp' => 'timestamp:Y-m-d H:i:s',
- ];
- protected static function onAfterInsert($model)
- {
- if ($model->weigh == 0) {
- $pk = $model->getPk();
- if (strlen($model[$pk]) >= 19) {
- $model->where($pk, $model[$pk])->update(['weigh' => $model->count()]);
- } else {
- $model->where($pk, $model[$pk])->update(['weigh' => $model[$pk]]);
- }
- }
- }
- public function getImagesAttr($value): array
- {
- if ($value === '' || $value === null) return [];
- if (!is_array($value)) {
- return explode(',', $value);
- }
- return $value;
- }
- public function setImagesAttr($value): string
- {
- return is_array($value) ? implode(',', $value) : $value;
- }
- public function getFilesAttr($value): array
- {
- if ($value === '' || $value === null) return [];
- if (!is_array($value)) {
- return explode(',', $value);
- }
- return $value;
- }
- public function setFilesAttr($value): string
- {
- return is_array($value) ? implode(',', $value) : $value;
- }
- public function getCheckboxAttr($value): array
- {
- if ($value === '' || $value === null) return [];
- if (!is_array($value)) {
- return explode(',', $value);
- }
- return $value;
- }
- public function setCheckboxAttr($value): string
- {
- return is_array($value) ? implode(',', $value) : $value;
- }
- public function getSelectsAttr($value): array
- {
- if ($value === '' || $value === null) return [];
- if (!is_array($value)) {
- return explode(',', $value);
- }
- return $value;
- }
- public function setSelectsAttr($value): string
- {
- return is_array($value) ? implode(',', $value) : $value;
- }
- public function getFloatAttr($value): float
- {
- return (float)$value;
- }
- public function getCityAttr($value): array
- {
- if ($value === '' || $value === null) return [];
- if (!is_array($value)) {
- return explode(',', $value);
- }
- return $value;
- }
- public function setCityAttr($value): string
- {
- return is_array($value) ? implode(',', $value) : $value;
- }
- public function getCityTextAttr($value, $row): string
- {
- if ($row['city'] === '' || $row['city'] === null) return '';
- $cityNames = \think\facade\Db::name('area')->whereIn('id', $row['city'])->column('name');
- return $cityNames ? implode(',', $cityNames) : '';
- }
- public function getArrayAttr($value): array
- {
- return !$value ? [] : json_decode($value, true);
- }
- public function setTimeAttr($value): string
- {
- return $value ? date('H:i:s', strtotime($value)) : '';
- }
- public function getEditorAttr($value): string
- {
- return !$value ? '' : htmlspecialchars_decode($value);
- }
- }
|