colly_wyx
2018-05-03 b8a82d561917a4336214225f65f4488d977c5fb1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/**
 * 数据库CURD基本操作示例
 * @author dogstar 20170612
 */
 
class Api_Examples_CURD extends PhalApi_Api {
 
    public function getRules() {
        return array(
            'insert' => array(
                'title' => array('name' => 'title', 'require' => true, 'min' => 1, 'max' => '20', 'desc' => '标题'),
                'content' => array('name' => 'content', 'require' => true, 'min' => 1, 'desc' => '内容'),
                'state' => array('name' => 'state', 'type' => 'int', 'default' => 0, 'desc' => '状态'),
            ),
            'update' => array(
                'id' => array('name' => 'id', 'require' => true, 'min' => 1, 'desc' => 'ID'),
                'title' => array('name' => 'title', 'require' => true, 'min' => 1, 'max' => '20', 'desc' => '标题'),
                'content' => array('name' => 'content', 'require' => true, 'min' => 1, 'desc' => '内容'),
                'state' => array('name' => 'state', 'type' => 'int', 'default' => 0, 'desc' => '状态'),
            ),
            'get' => array(
                'id' => array('name' => 'id', 'require' => true, 'min' => 1, 'desc' => 'ID'),
            ),
            'delete' => array(
                'id' => array('name' => 'id', 'require' => true, 'min' => 1, 'desc' => 'ID'),
            ),
            'getList' => array(
                'page' => array('name' => 'page', 'type' => 'int', 'min' => 1, 'default' => 1, 'desc' => '第几页'),
                'perpage' => array('name' => 'perpage', 'type' => 'int', 'min' => 1, 'max' => 20, 'default' => 10, 'desc' => '分页数量'),
                'state' => array('name' => 'state', 'type' => 'int', 'default' => 0, 'desc' => '状态'),
            ),
        );
    }
 
    /**
     * 插入数据
     * @desc 向数据库插入一条纪录数据
     * @return int id 新增的ID
     */
    public function insert() {
        $rs = array();
 
        $newData = array(
            'title' => $this->title,
            'content' => $this->content,
            'state' => $this->state,
        );
 
        $domain = new Domain_Examples_CURD();
        $id = $domain->insert($newData);
 
        $rs['id'] = $id;
        return $rs; 
    }
 
    /**
     * 更新数据
     * @desc 根据ID更新数据库中的一条纪录数据
     * @return int code 更新的结果,1表示成功,0表示无更新,false表示失败
     */
    public function update() {
        $rs = array();
 
        $newData = array(
            'title' => $this->title,
            'content' => $this->content,
            'state' => $this->state,
        );
 
        $domain = new Domain_Examples_CURD();
        $code = $domain->update($this->id, $newData);
 
        $rs['code'] = $code;
        return $rs;
    }
 
    /**
     * 获取数据
     * @desc 根据ID获取数据库中的一条纪录数据
     * @return int      id          主键ID
     * @return string   title       标题
     * @return string   content     内容
     * @return int      state       状态
     * @return string   post_date   发布日期
     */
    public function get() {
        $domain = new Domain_Examples_CURD();
        $data = $domain->get($this->id);
 
        return $data;
    }
 
    /**
     * 删除数据
     * @desc 根据ID删除数据库中的一条纪录数据
     * @return int code 删除的结果,1表示成功,0表示失败
     */
    public function delete() {
        $rs = array();
 
        $domain = new Domain_Examples_CURD();
        $code = $domain->delete($this->id);
 
        $rs['code'] = $code;
        return $rs;
    }
 
    /**
     * 获取分页列表数据
     * @desc 根据状态筛选列表数据,支持分页
     * @return array    items   列表数据
     * @return int      total   总数量
     * @return int      page    当前第几页
     * @return int      perpage 每页数量
     */
    public function getList() {
        $rs = array();
 
        $domain = new Domain_Examples_CURD();
        $list = $domain->getList($this->state, $this->page, $this->perpage);
 
        $rs['items'] = $list['items'];
        $rs['total'] = $list['total'];
        $rs['page'] = $this->page;
        $rs['perpage'] = $this->perpage;
 
        return $rs;
    }
}