ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

hyperf 数据库模型-修改器

2021-10-01 08:31:07  阅读:179  来源: 互联网

标签:01 07 数据库 修改器 hyperf 2021 NULL id user


访问器

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
}

标签:01,07,数据库,修改器,hyperf,2021,NULL,id,user
来源: https://www.cnblogs.com/hu308830232/p/15358705.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有