hyperf 数据库模型-修改器

2021/10/1 19:14:26

本文主要是介绍hyperf 数据库模型-修改器,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

访问器

Index控制器 app/Controller/IndexController.php

<?php
namespace App\Controller;

use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Annotation\AutoController;
use App\Model\User;

/**
 * @AutoController();
 */
class IndexController
{
        public function index(RequestInterface $request){
                $id = (int)$request->input('id',1);
                $user = User::query()->where('id',$id)->first();
                return $user->name.PHP_EOL;
        }

}

User模型 app/Model/User.php

<?php

declare (strict_types=1);
namespace App\Model;

use Hyperf\DbConnection\Model\Model;

class User extends Model
{

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'user';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [];

    /**
     * 访问器示例
     * @param  string  $value
     * @return string
     */
    public function getNameAttribute($value)
    {
        return strtoupper($value);
    }
}

user表数据

mysql> select * from user;
+----+------------------+------+---------+---------+--------+
| id | name             | age  | role_id | address | status |
+----+------------------+------+---------+---------+--------+
|  1 | xiaohong         |   30 |       2 | NULL    |      1 |
|  2 | huyongjian2      |   24 |       2 | NULL    |      0 |
|  4 | xiaoming         |   28 |       2 | NULL    |      1 |
|  5 | xiaoming5        |   30 |       2 | NULL    |      1 |
|  6 | huyongjian1      |   30 |       2 | NULL    |      1 |
|  7 | huyongjian2      |   31 |       2 | NULL    |      1 |
|  8 | xiaohong         |   24 |       1 | NULL    |      1 |
| 11 | model_event_test |   20 |       1 | NULL    |      1 |
+----+------------------+------+---------+---------+--------+
8 rows in set (0.00 sec)

测试访问(name变大写)

curl 118.195.173.53:9501/index/index?id=1

返回结果

XIAOMING

修改器

Index控制器 app/Controller/IndexController.php

<?php
namespace App\Controller;

use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Annotation\AutoController;
use App\Model\User;

/**
 * @AutoController();
 */
class IndexController
{
        public function index(RequestInterface $request){
                $id = (int)$request->input('id',1);
                $user = User::query()->where('id',$id)->first();
                $user->name = HUYONGJIAN;
                $user->save();
                return $user->name . PHP_EOL;
        }

}

User模型 app/Model/User.php

<?php

declare (strict_types=1);
namespace App\Model;

use Hyperf\DbConnection\Model\Model;

class User extends Model
{

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'user';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [];

    public $timestamps = false;

    /**
     * 修改器示例
     */
    public function setNameAttribute($value){
        $this->attributes['name'] = strtolower($value);
    }
}

user表数据

mysql> select * from user;
+----+------------------+------+---------+---------+--------+
| id | name             | age  | role_id | address | status |
+----+------------------+------+---------+---------+--------+
|  1 | xiaohong         |   30 |       2 | NULL    |      1 |
|  2 | huyongjian2      |   24 |       2 | NULL    |      0 |
|  4 | xiaoming         |   28 |       2 | NULL    |      1 |
|  5 | xiaoming5        |   30 |       2 | NULL    |      1 |
|  6 | huyongjian1      |   30 |       2 | NULL    |      1 |
|  7 | huyongjian2      |   31 |       2 | NULL    |      1 |
|  8 | xiaohong         |   24 |       1 | NULL    |      1 |
| 11 | model_event_test |   20 |       1 | NULL    |      1 |
+----+------------------+------+---------+---------+--------+
8 rows in set (0.00 sec)

测试访问(name转变成小写,并保存)

curl 118.195.173.53:9501/index/index?id=1

返回

huyongjian

测试2

curl 118.195.173.53:9501/index/index?id=2

返回

huyongjian

再次查看user表数据,id=1,2的name都是小写

mysql> select * from user;
+----+------------------+------+---------+---------+--------+
| id | name             | age  | role_id | address | status |
+----+------------------+------+---------+---------+--------+
|  1 | huyongjian       |   30 |       2 | NULL    |      1 |
|  2 | huyongjian       |   24 |       2 | NULL    |      0 |
|  4 | xiaoming         |   28 |       2 | NULL    |      1 |
|  5 | xiaoming5        |   30 |       2 | NULL    |      1 |
|  6 | huyongjian1      |   30 |       2 | NULL    |      1 |
|  7 | huyongjian2      |   31 |       2 | NULL    |      1 |
|  8 | xiaohong         |   24 |       1 | NULL    |      1 |
| 11 | model_event_test |   20 |       1 | NULL    |      1 |
+----+------------------+------+---------+---------+--------+
8 rows in set (0.00 sec)

日期转化器

控制器 app/Controller/IndexController.php

<?php
namespace App\Controller;

use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Annotation\AutoController;
use App\Model\User;

/**
 * @AutoController();
 */
class IndexController
{
        public function index(RequestInterface $request){
                $id = (int)$request->input('id',1);
                $user = User::query()->where('id',$id)->first();
                //time()返回时间戳(1633045072)
                $user->date_time = time();
                $user->save();
                return [
                        'date_time' =>$user->date_time,
                        'time_stamp' => $user->date_time->getTimestamp()
                ];
        }

}

User模型 app/Model/User.php

<?php

declare (strict_types=1);
namespace App\Model;

use Hyperf\DbConnection\Model\Model;

class User extends Model
{

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'user';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [];

    public $timestamps = false;

    /**
     * 日期转换器字段配置
     *
     * @var array
     */
    protected $dates = [
        'date_time',
    ];
}

user表数据

mysql> select * from user;
+----+------------------+------+---------+---------+---------------------+--------+
| id | name             | age  | role_id | address | date_time           | status |
+----+------------------+------+---------+---------+---------------------+--------+
|  1 | huyongjian       |   30 |       2 | NULL    | 2021-10-01 07:37:51 |      1 |
|  2 | huyongjian       |   24 |       2 | NULL    | 2021-10-01 07:37:51 |      0 |
|  4 | xiaoming         |   28 |       2 | NULL    | 2021-10-01 07:37:51 |      1 |
|  5 | xiaoming5        |   30 |       2 | NULL    | 2021-10-01 07:37:51 |      1 |
|  6 | huyongjian1      |   30 |       2 | NULL    | 2021-10-01 07:37:51 |      1 |
|  7 | huyongjian2      |   31 |       2 | NULL    | 2021-10-01 07:37:51 |      1 |
|  8 | xiaohong         |   24 |       1 | NULL    | 2021-10-01 07:37:51 |      1 |
| 11 | model_event_test |   20 |       1 | NULL    | 2021-10-01 07:37:51 |      1 |
+----+------------------+------+---------+---------+---------------------+--------+
8 rows in set (0.00 sec)

访问测试

curl 118.195.173.53:9501/index/index?id=1

返回结果

{
    "date_time": "2021-09-30T23:53:00.000000Z",
    "time_stamp": 1633045980
}

更新后的user表数据(id=1记录date_time字段已自动转换成日期时间类型)

mysql> select * from user;
+----+------------------+------+---------+---------+---------------------+--------+
| id | name             | age  | role_id | address | date_time           | status |
+----+------------------+------+---------+---------+---------------------+--------+
|  1 | huyongjian       |   30 |       2 | NULL    | 2021-10-01 07:53:00 |      1 |
|  2 | huyongjian       |   24 |       2 | NULL    | 2021-10-01 07:37:51 |      0 |
|  4 | xiaoming         |   28 |       2 | NULL    | 2021-10-01 07:37:51 |      1 |
|  5 | xiaoming5        |   30 |       2 | NULL    | 2021-10-01 07:37:51 |      1 |
|  6 | huyongjian1      |   30 |       2 | NULL    | 2021-10-01 07:37:51 |      1 |
|  7 | huyongjian2      |   31 |       2 | NULL    | 2021-10-01 07:37:51 |      1 |
|  8 | xiaohong         |   24 |       1 | NULL    | 2021-10-01 07:37:51 |      1 |
| 11 | model_event_test |   20 |       1 | NULL    | 2021-10-01 07:37:51 |      1 |
+----+------------------+------+---------+---------+---------------------+--------+
8 rows in set (0.00 sec)

时间格式

控制器 app/Controller/IndexController.php

<?php
namespace App\Controller;

use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Annotation\AutoController;
use App\Model\User;

/**
 * @AutoController();
 */
class IndexController
{
        public function index(RequestInterface $request){
                $id = (int)$request->input('id',1);
                $user = User::query()->where('id',$id)->first();
                return [
                        'status' =>$user->status,
                ];
        }

}

User模型 app/Model/User.php

<?php

declare (strict_types=1);
namespace App\Model;

use Hyperf\DbConnection\Model\Model;

class User extends Model
{

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'user';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'status'=>'boolean'
    ];

    public $timestamps = false;

}

注:支持转换的数据类型有:integer, real, float, double, decimal:, string, boolean, object, array, collection, date, datetime 和 timestamp。 当需要转换为 decimal 类型时,你需要定义小数位的个数,如: decimal:2

user数据表

mysql> select * from user;
+----+------------------+------+---------+---------+---------------------+--------+
| id | name             | age  | role_id | address | date_time           | status |
+----+------------------+------+---------+---------+---------------------+--------+
|  1 | huyongjian       |   30 |       2 | NULL    | 2021-10-01 07:53:00 |      1 |
|  2 | huyongjian       |   24 |       2 | NULL    | 2021-10-01 07:37:51 |      0 |
|  4 | xiaoming         |   28 |       2 | NULL    | 2021-10-01 07:37:51 |      1 |
|  5 | xiaoming5        |   30 |       2 | NULL    | 2021-10-01 07:37:51 |      1 |
|  6 | huyongjian1      |   30 |       2 | NULL    | 2021-10-01 07:37:51 |      1 |
|  7 | huyongjian2      |   31 |       2 | NULL    | 2021-10-01 07:37:51 |      1 |
|  8 | xiaohong         |   24 |       1 | NULL    | 2021-10-01 07:37:51 |      1 |
| 11 | model_event_test |   20 |       1 | NULL    | 2021-10-01 07:37:51 |      1 |
+----+------------------+------+---------+---------+---------------------+--------+
8 rows in set (0.00 sec)

访问测试(status字段0或1会被转换false或true)

curl 118.195.173.53:9501/index/index?id=1

返回结果

{
    "status": true
}


这篇关于hyperf 数据库模型-修改器的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程