-
Notifications
You must be signed in to change notification settings - Fork 0
/
PdoEntity.php
148 lines (139 loc) · 2.84 KB
/
PdoEntity.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/**
* @file PdoEntity.php
*
* PdoEntity
*
* copyright (c) 2006-2017 Frank Hellenkamp [jonas@depage.net]
*/
namespace Depage\Entity;
abstract class PdoEntity extends Entity
{
/**
* @brief pdo object for database access
**/
protected $pdo = null;
// {{{ __construct()
/**
* @brief __construct
*
* @param mixed $pdo, $user
* @return void
**/
public function __construct(\Depage\Db\Pdo $pdo)
{
parent::__construct($pdo);
$this->pdo = $pdo;
}
// }}}
// abstract functions
// {{{ loadBy()
/**
* @brief loadBy
*
* @param mixed $
* @return void
**/
static public function loadBy($pdo, Array $search, Array $order = []) {
}
// }}}
// {{{ save()
/**
* @brief save
*
* @param mixed
* @return void
**/
abstract public function save();
// }}}
// empty overridable class functions
// {{{ onLoad()
/**
* @brief onLoad
*
* @param mixed
* @return void
**/
protected function onLoad()
{
}
// }}}
// {{{ onSave()
/**
* @brief onSave
*
* @param mixed
* @return void
**/
protected function onSave()
{
}
// }}}
// helpers
// {{{ sqlConditionFor()
/**
* @brief sqlConditionFor
*
* @param mixed $name, $values
* @return void
**/
protected static function sqlConditionFor($name, $values, &$params)
{
$escapedName = str_replace(".", "_", $name);
if (!is_array($values)) {
$params[$escapedName] = $values;
return "$name = :$escapedName";
} else {
$where = "$name IN (";
foreach ($values as $key => $val) {
$params["$escapedName$key"] = $val;
$where .= ":$escapedName$key,";
}
return rtrim($where, ",") . ")";
}
}
// }}}
// {{{ dateTimestamp()
/**
* @brief
*
* @param mixed $timestamp = null
* @return void
**/
public static function dateTimestamp($timestamp = null)
{
if ($timestamp === null) {
$timestamp = time();
}
return date('Y-m-d H:i:s', $timestamp);
}
// }}}
// {{{ escapeLike()
/**
* @brief escapeLike
*
* @param mixed $s, $e
* @return void
**/
static public function escapeLike($s, $e)
{
return str_replace([$e, '_', '%'], ["{$e}{$e}", "{$e}_", "{$e}%"], $s);
}
// }}}
// {{{ __sleep()
/**
* allows Depage\Db\Pdo-object to be serialized
*/
public function __sleep()
{
return array(
'pdo',
'initialized',
'data',
'types',
'dirty',
);
}
// }}}
}
// vim:set ft=php sw=4 sts=4 fdm=marker et :