但行好事,莫问前程

挖了太多坑,一点点填回来

为CakePHP的Model增加链式操作

cakephp, php

CakePHP中Model默认的查询方法不够灵活,我hack了一下,增加链式操作方式。

(AppModel.php) download
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
<?php
class AppModel extends Model {
    public function save($data=null, $validate=true, $fieldList=array()) {
        if (isset($this->data) && isset($this->data[$this->name])) {
            unset($this->data[$this->name]['modified']);
        }
        if (isset($data) && isset($data[$this->name])) {
            unset($data[$this->name]['modified']);
        }
        return parent::save($data, $validate, $fieldList);
    }

    public function relation($params) {
        $this->_params['recursive'] = $params;
        return $this;
    }

    public function where(array $params) {
        $this->_params['conditions'] = $params;
        return $this;
    }

    public function order($params) {
        $this->_params['order'] = $params;
        return $this;
    }

    public function fields($params) {
        $this->_params['fields'] = $params;
        return $this;
    }

    public function page($params) {
        $this->_params['page'] = $params;
        return $this;
    }

    public function limit($params) {
        $this->_params['limit'] = $params;
        return $this;
    }

    public function offset($params) {
        $this->_params['offset'] = $params;
        return $this;
    }

    public function joins($params) {
        $this->_params['joins'] = $params;
        return $this;
    }

    public function group($params) {
        $this->_params['group'] = $params;
        return $this;
    }

    public function count() {
        $r = $this->find('count', $this->_params);
        $this->_params = array();
        return $r;
    }

    public function first() {
        $r = $this->find('first', $this->_params);
        $this->_params = array();
        return $r;
    }

    public function select() {
        $r = $this->find('all', $this->_params);
        $this->_params = array();
        return $r;
    }

    public function lists() {
        $r = $this->find('list', $this->_params);
        $this->_params = array();
        return $r;
    }

    public function threaded() {
        $r = $this->find('threaded', $this->_params);
        $this->_params = array();
        return $r;
    }

    public function neighbors() {
        $r = $this->find('neighbors', $this->_params);
        $this->_params = array();
        return $r;
    }

    public function getConditions() {
        if (isset($this->_params['conditions'])) {
            return $this->_params['conditions'];
        }
        return array();
    }

    protected function _appendConditions($params) {
        if (!isset($this->_params['conditions'])) {
            $this->_params['conditions'] = $params;
        } else {
            $this->_params['conditions'] = am($this->_params['conditions'], $params);
        }
        return $this;
    }
}

Have a nice day!