tp5获取器案例

<?php


namespace app\api\controller\v1;

use app\api\model\Test as TestModel;

class Test
{
    public function index()
    {
        //输出董燃个人信息
        $test=TestModel::get(9);
        return $test->name."的入职时间是:".$test->hiredate;
    }

}

结果:

"董燃的入职时间是:1563978579"
<?php


namespace app\api\controller\v1;

use app\api\model\Test as TestModel;

class Test
{
    public function index()
    {
        //输出董燃个人信息
        $test=TestModel::get(9);
        return $test->name."的入职时间是:".date('Y年m月d日 H时i分s秒',$test->hiredate);
    }

}

结果:

"董燃的入职时间是:2019年07月24日 22时29分39秒"

这种操作每一次都要写会很麻烦,可以在模型中设置读取器方法一次搞定

Controller

<?php


namespace app\api\controller\v1;

use app\api\model\Test as TestModel;

class Test
{
    public function index()
    {
        //输出董燃个人信息
        $test=TestModel::get(9);
        return $test->name."的入职时间是:".$test->hiredate;
    }

}

Model

<?php


namespace app\api\model;


class Test extends BaseModel
{
    protected function gethireDateAttr($hiredate)
    {
        return date('Y年m月d日 H时i分s秒',$hiredate);
    }
}

只需要在模型中定义一次就可以了,非常方便。

获取器还支持传入第二个参数$data

Controller

<?php


namespace app\api\controller\v1;

use app\api\model\Test as TestModel;

class Test
{
    public function index()
    {
        $test=TestModel::get(9);
        return $test->hiredate;
    }

}

Model

<?php


namespace app\api\model;


class Test extends BaseModel
{
    protected function gethireDateAttr($hiredate,$data)
    {
        //data保存着当前模型所有的原始数据,data['name']即对应着数据表中的name字段
        return $data['name']."的入职时间是:".date('Y年m月d日 H时i分s秒',$hiredate);
    }
    protected function sethireDateAttr($hiredate)
    {
        return strtotime($hiredate);
    }
}

 

"董燃的入职时间是:2019年07月24日 22时29分39秒"

 

<?php


namespace app\api\controller\v1;

use app\api\model\Test as TestModel;

class Test
{
    public function index()
    {
        $test=new TestModel();
        $test->name="冷风";
        $test->sex=0;
        $test->hiredate=strtotime('2019-7-24');
        if($test->save())
        {
            return "新员工".$test->name."ID:".$test->id."添加成功";
        }
        else
        {
            $test->getError();
        }
    }

}
"新员工冷风ID:10添加成功"

 非常麻烦,

在模型中设置设置器,使得时间字符串自动转化为unix时间戳

Controller

<?php


namespace app\api\controller\v1;

use app\api\model\Test as TestModel;

class Test
{
    public function index()
    {
        $test=new TestModel();
        $test->name="冷风";
        $test->sex=0;
        $test->hiredate='2019-7-24';
        if($test->save())
        {
            return "新员工".$test->name."ID:".$test->id."添加成功";
        }
        else
        {
            $test->getError();
        }
    }

}

Model

<?php

namespace app\api\model;


class Test extends BaseModel
{
    protected function gethireDateAttr($hiredate)
    {
        return date('Y年m月d日 H时i分s秒',$hiredate);
    }
    protected function sethireDateAttr($hiredate)
    {
        return strtotime($hiredate);
    }
}
"新员工冷风ID:11添加成功"

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值