Blogger Information
Blog 8
fans 0
comment 0
visits 7381
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
5.25 实例获取器和修改器及验证器的用法代码实现
Allen的php博客
Original
735 people have browsed it

一、准备用户验证的数据表:user

mysql.png

二、创建获取器和修改器模型

app\model\User.php

获取器和修改器.png


<?php

namespace app\model;

use think\Model;

use think\model\concern\SoftDelete;


class User extends Model

{

//设置数据库的名称

protected $table = 'user';


//设置默认主键

protected $pk = 'id';


//软删除:1.复制SoftDelete类到当前模型;2.添加软删除deleteTime属性; 3.设置软删除默认字段值

use SoftDelete;

protected $delete_time = 'delete_time';

    protected $defaultSoftDelete = 0;


    //获取器用法1:sex,0=>男,1=>女

    protected function getSexAttr($value)

    {

    $sex = [0=>'男',1=>'女'];

    return $sex[$value];

    }


    //获取器用法2:传入第二个参数,引用全部的字段值

    protected function getNickNameAttr($value, $data)

    {

    return $value.'-------'.$data['name'].'的昵称';

    }


    //获取器用法3:可以为表不存在的字段设置,纯粹的拼装字段内容

    protected function getUserInfoAttr($value, $data)

    {

    //user_info:虚拟字段

    //$value:仅仅是一个占位

    return '我是'.$data['name'].',我的昵称是:'.$data['nick_name'].'。我的登录邮箱是:'.$data['email'];


    }


    //修改器试用方法1

    protected function setCreateTimeAttr($value)

    {

    return strtotime($value);

    }


    //修改器试用方法2:支持第二个参数,引用全部字段的值

    protected function setNickNameAttr($value, $data)

    {

    return $value;

    }


    //类型转换

    protected $type = [

    'id' => 'integer',

    'uid' => 'integer',

    'sex' => 'integer',

    'create_time' => 'integer',

    'delete_time' => 'integer'

    ];


    //自动完成:主要针对写操作,新增、更新

    //相当于给字段设置默认值

    protected $insert = [

    'login_type'=>0,

    'nick_name'=>'小情绪'

    ]; //针对新增

    protected $update = []; //针对更新

    protected $auto = ['sex'=>0]; //针对新增和更新


    //开启当前模型的自动时间戳功能

    protected $autoWriteTimestamp = true;


    //设置一下用户自定义的新增和更新时间的字段名

    protected $createTime = 'create_time';

    protected $updateTime = 'update_time';


}

app\admin\controller\Register.php

获取和修改控制器.png

<?php

namespace app\admin\controller;

use think\Controller;

use app\model\User;


class Register extends Controller

{

//获取器

public function gain()

{

$res = User::get(2); 

// \var_dump($res);

echo $res->sex,'<br>';

// echo $res->getData('sex'),'<br>'; //获取数据原始值

echo $res->nick_name,'<br>';

echo $res->user_info,'<br>';


}


//修改器

public function modify()

{

//保存时间戳,转换格式

$res = User::get(3);

$res->create_time = '2018-05-26';

$res->save();


//修改用户昵称

$res = User::get(4);

$res->nick_name = '信春哥、得永生';

$res->save();


return '修改成功!';

}


//自动完成

public function auto()

{

//创建数据,创建时间就是更新时间

User::create([

'name'=>'萧十一郎',

'password'=>md5(12345),

'nick_name'=>'潇洒英俊小鲜肉',

'email'=>'32421313@qq.com

']);

// 改变更新时间

User::update(

['nick_name'=>'昨晚很嗨啊'],

function($query){

$query->where('id','=',25);

}

);

return '操作成功';

}

}

三、创建验证器模块及其控制器

验证器:app\validate\User.php

验证器.png


<?php

namespace app\index\validate;

use think\Validate;


class User extends Validate

{

//验证规则,$rule等名称要按要求来写,不能随便命名

protected $rule = [

'name' => 'require|length:4,15',

'sex' => 'in:0,1',

'password' => 'require|length:6,15',

'email' => 'email'

];


//错误信息

protected $message = [

'name.require' => '姓名不能为空',

'name.length' => '姓名必须在4到15个字符之间',

'sex.in' => '性别必须是男或女',

'password.require' => '密码不能为空',

'password.length' => '密码长度必须在6到15个字符之间',

'email.email' => '邮箱格式不正确'

];


}

控制器:app\index\controller\Verify.php

验证器控制器.png

<?php

namespace app\index\controller;

use think\Controller;

use app\index\validate\User; //导入验证器类

use think\Validate;


class Verify extends Controller

{

//验证器

public function demo1(User $user) //依赖注入

{

//准备一下验证的数据

$data = [

'name' => '李生才1111',

'sex' => '1',

'password' => 'fafa231313',

'email' => '113131211'

];

if(!$user->check($data)){

dump($user->getError());

}else{

echo '验证成功';

}

}

//验证器的简化:$this->validate(数据,验证规则,错误信息)

public function demo2()

{

//准备一下验证的数据

$data = [

'name' => '王小琳1111',

'sex' => '1',

'password' => '111313',

'email' => '113131211@qq.com'

];

//导入验证规则

$rule = 'app\index\validate\User';

$res = $this->validate($data, $rule);

if(true !== $res){

return $res;

}else{

return '验证成功';

}

}


//独立验证

public function demo3()

{

//独立验证是直接实例化think\Validate.php中的make(),将验证规则和错误信息写在控制器中

//验证器是自定义的验证方法,个人认为自定义验证器更加规范和灵活,更具有复用性


//1. 创建验证规则

$rule = ['email' => 'require|email'];


//2. 创建错误信息

$mess = [

'email.require' => '邮箱不能为空',

'email.email' => '邮箱格式不正确,请检查'

];


//3. 创建数据

$data = ['email' => '123131234@qq.com'];


//Validate::make(验证规则,错误信息):创建验证规则并返回验证对象

$validate = Validate::make($rule, $mess); //需要引入think\Validate


//验证:$validate->check($data)

$res = $validate->check($data);


return $res ? '验证成功' : $validate->getError();


}


}

以上代码均经过测试通过,可用

Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!