JustDoItTomorrow
5/5/2018 - 3:00 PM

模型

<?php
namespace app\index\controller;

use app\index\model\User as UserModel;

class User
{
    /**
     * 增加一个
     */
    public function add()
    {
        $user = new UserModel;
        $user->nickname = '流年';
        $user->email = 'thinkphpp';
        $user->birthday = strtotime('1977-03-05');
        if ($user->save()) {
            return "用户[$user->nickname:$user->id]新增成功";
        } else {
            return $user->getError();
        }
    }

    /**
     * 增加多个
     */
    public function addList()
    {
        $user = new UserModel;
        $list = [
            ['nickname' => '张三', 'email' => 'zhangsan@qq.com', 'birthday' =>strtotime('1988-01-15')],
            ['nickname' => '李四', 'email' => 'lisi@qq.com', 'birthday' => strtotime('1990-09-19')]
        ];
        if ($user->saveAll($list)) {
            return '用户批量新增成功';
        } else {
            return $user->getError();
        }
    }

    /**
     * 通过主键读取
     */
    public function read($id = '')
    {
        $user = UserModel::get($id);
        echo $user->nickname . '<br/>';
        echo $user->email . '<br/>';
        echo date('Y/m/d', $user->birthday) . '<br/>';
    }

    /**
     * 通过nickname读取
     */
    public function readByName()
    {
        $user = UserModel::get(['nickname' => '张三']);
        echo $user->nickname . '<br/>';
        echo $user->email . '<br/>';
        echo date('Y/m/d', $user->birthday) . '<br/>';
    }

    /**
     * 获取所有数据
     */
    public function index()
    {
        $list = UserModel::all();
        foreach ($list as $user) {
            echo $user->nickname . '<br/>';
            echo $user->email . '<br/>';
            echo date('Y/m/d', $user->birthday) . '<br/>';
        }
    }

    /**
     * 获取所有数据,通过status筛选
     */
    public function indexByStatus()
    {
        $list = UserModel::all(['status'=>0]);
        foreach ($list as $user) {
            echo $user->nickname . '<br/>';
            echo $user->email . '<br/>';
            echo date('Y/m/d', $user->birthday) . '<br/>';
            echo '----------------------------------<br/>';
        }
    }

    /**
     * 获取所有数据,通过id筛选
     */
    public function indexById()
    {
        $list = UserModel::all(function($query) {
            $query->where('id', '<', 3)->order('id', 'desc');
        });
        foreach($list as $user) {
            echo $user->nickname . '<br/>';
            echo $user->email . '<br/>';
            echo date('Y/m/d', $user->birthday) . '<br/>';
        }
    }

    /**
     * 更新数据
     */
    public function update($id)
    {
        $user = UserModel::get($id);
        $user->nickname = '刘晨';
        $user->email = 'liu21st@gmail.com';
        $user->save();
        return '更新用户成功';
    }

    /**
     * 删除用户
     */
    public function delete($id)
    {
        $user = UserModel::get($id);
        if ($user) {
            $user->delete();
            return '删除用户成功';
        } else {
            return '删除的用户不存在';
        }
    }

    /**
     * 使用destroy删除用户
     */
    public function delete2($id)
    {
        $user = UserModel::destory($id);
        if ($result) {
            return '删除用户成功';
        } else {
            return '删除的用户不存在';
        }
    }
    
    /**
     * birthday读取器 
     */
    protected function getBirthdayAttr($birthday)
    {
        return date('Y-m-d', $birthday);
    }
    
    /**
     * use_birthday读取器,user_birthday是不存在的属性,所以这里用$data参数来获取原本的值
     */
    protected function getUserBirthdayAttr($value, $data)
    {
        return date('Y-m-d', $data['birthday']);
    }
    
    /**
     * birthday修改器
     */
    protected function setBirthdayAttr($value)
    {
        return strtotime($value);
    }
}