forked from croogo/croogo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_model.php
121 lines (118 loc) · 3.78 KB
/
app_model.php
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
<?php
/**
* Application model
*
* This file is the base model of all other models
*
* PHP version 5
*
* @category Models
* @package Croogo
* @version 1.0
* @author Fahad Ibnay Heylaal <contact@fahad19.com>
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
* @link http://www.croogo.org
*/
class AppModel extends Model {
/**
* use Caching
*
* @var string
*/
public $useCache = true;
/**
* Constructor
*
* @param mixed $id Set this ID for this model on startup, can also be an array of options, see above.
* @param string $table Name of database table to use.
* @param string $ds DataSource connection name.
*/
public function __construct($id = false, $table = null, $ds = null) {
Croogo::applyHookProperties('Hook.model_properties');
parent::__construct($id, $table, $ds);
}
/**
* Override find function to use caching
*
* Caching can be done either by unique names,
* or prefixes where a hashed value of $options array is appended to the name
*
* @param mixed $type
* @param array $options
* @return mixed
* @access public
*/
public function find($type, $options = array()) {
if ($this->useCache) {
$cachedResults = $this->_findCached($type, $options);
if ($cachedResults) {
return $cachedResults;
}
}
$args = func_get_args();
$results = call_user_func_array(array('parent', 'find'), $args);
if ($this->useCache) {
if (isset($options['cache']['name']) && isset($options['cache']['config'])) {
$cacheName = $options['cache']['name'];
} elseif (isset($options['cache']['prefix']) && isset($options['cache']['config'])) {
$cacheName = $options['cache']['prefix'] . md5(serialize($options));
}
if (isset($cacheName)) {
$cacheName .= '_' . Configure::read('Config.language');
Cache::write($cacheName, $results, $options['cache']['config']);
}
}
return $results;
}
/**
* Check if find() was already cached
*
* @param mixed $type
* @param array $options
* @return void
* @access private
*/
function _findCached($type, $options) {
if (isset($options['cache']['name']) && isset($options['cache']['config'])) {
$cacheName = $options['cache']['name'];
} elseif (isset($options['cache']['prefix']) && isset($options['cache']['config'])) {
$cacheName = $options['cache']['prefix'] . md5(serialize($options));
} else {
return false;
}
$cacheName .= '_' . Configure::read('Config.language');
$results = Cache::read($cacheName, $options['cache']['config']);
if ($results) {
return $results;
}
return false;
}
/**
* Updates multiple model records based on a set of conditions.
*
* call afterSave() callback after successful update.
*
* @param array $fields Set of fields and values, indexed by fields.
* Fields are treated as SQL snippets, to insert literal values manually escape your data.
* @param mixed $conditions Conditions to match, true for all records
* @return boolean True on success, false on failure
* @access public
*/
public function updateAll($fields, $conditions = true) {
$args = func_get_args();
$output = call_user_func_array(array('parent', 'updateAll'), $args);
if ($output) {
$created = false;
$options = array();
$this->Behaviors->trigger($this, 'afterSave', array(
$created,
$options,
));
$this->afterSave($created);
$this->_clearCache();
return true;
}
return false;
}
}
?>