Rev 27 | Rev 34 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
27 | PointedEar | 1 | <?php |
2 | |||
29 | PointedEar | 3 | require_once __DIR__ . '/../AbstractModel.php'; |
27 | PointedEar | 4 | |
5 | /** |
||
6 | * Generic database table model class |
||
7 | * |
||
8 | * @author Thomas Lahn |
||
9 | * @property-read int $lastInsertId |
||
10 | * ID of the last inserted row, or the last value from |
||
11 | a sequence object, depending on the underlying driver. |
||
12 | */ |
||
13 | abstract class Table extends AbstractModel |
||
14 | { |
||
15 | /** |
||
16 | * Name of the table |
||
17 | */ |
||
18 | protected $_name = ''; |
||
19 | |||
20 | /** |
||
21 | * Database of the table |
||
22 | * @var Database |
||
23 | */ |
||
24 | protected $_database; |
||
25 | |||
26 | protected $_id = 'id'; |
||
27 | |||
28 | public function __construct() |
||
29 | { |
||
30 | $this->_database = Application::getInstance()->getDefaultDatabase(); |
||
31 | } |
||
32 | |||
33 | /** |
||
34 | * Returns the database for the table |
||
35 | * @return Database |
||
36 | */ |
||
37 | public function getDatabase() |
||
38 | { |
||
39 | return $this->_database; |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Initiates a transaction |
||
44 | * |
||
45 | * @return bool |
||
46 | * @see Database::beginTransaction() |
||
47 | */ |
||
48 | public function beginTransaction() |
||
49 | { |
||
50 | return $this->_database->beginTransaction(); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Rolls back a transaction |
||
55 | * |
||
56 | * @return bool |
||
57 | * @see Database::rollBack() |
||
58 | */ |
||
59 | public function rollBack() |
||
60 | { |
||
61 | return $this->_database->rollBack(); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Commits a transaction |
||
66 | * |
||
67 | * @return bool |
||
68 | * @see Database::commit() |
||
69 | */ |
||
70 | public function commit() |
||
71 | { |
||
72 | return $this->_database->commit(); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Retrieves all rows from the table |
||
77 | * |
||
78 | * @return array |
||
79 | * @see Database::fetchAll() |
||
80 | */ |
||
81 | public function fetchAll($fetch_style = null, $column_index = null, array $ctor_args = null) |
||
82 | { |
||
83 | return $this->_database->fetchAll($this->_name, $fetch_style, $column_index, $ctor_args); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Selects data from one or more tables |
||
88 | * |
||
89 | * @return array |
||
90 | * @see Database::select() |
||
91 | */ |
||
92 | public function select($columns = null, $where = null, $order = null, $limit = null) |
||
93 | { |
||
94 | return $this->_database->select($this->_name, $columns, $where, $order, $limit); |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Updates records in one or more tables |
||
99 | * |
||
100 | * @return bool |
||
101 | * @see Database::update() |
||
102 | */ |
||
103 | public function update($data, $condition) |
||
104 | { |
||
105 | return $this->_database->update($this->_name, $data, $condition); |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Inserts a record into the table |
||
110 | * |
||
111 | * @return bool |
||
112 | * @see Database::insert() |
||
113 | */ |
||
114 | public function insert($data, $cols = null) |
||
115 | { |
||
116 | return $this->_database->insert($this->_name, $data, $cols); |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Returns the ID of the last inserted row, or the last value from |
||
121 | * a sequence object, depending on the underlying driver. |
||
122 | * |
||
123 | * @return int |
||
124 | * @see Database::getLastInsertId() |
||
125 | */ |
||
126 | public function getLastInsertId() |
||
127 | { |
||
128 | return $this->_database->lastInsertId; |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Delete a record from the table |
||
133 | * |
||
134 | * @param int $id |
||
135 | * ID of the record to delete. May be <code>null</code>, |
||
136 | * in which case <var>$condition</var> must specify |
||
137 | * the records to be deleted. |
||
138 | * @param array[optional] $condition |
||
139 | * Conditions that must be met for a record to be deleted. |
||
140 | * Ignored if <var>$id</var> is not <code>null</code>. |
||
141 | * @return bool |
||
142 | * @throws InvalidArgumentException if both <var>$id</var> and |
||
143 | * <var>$condition</var> are <code>null</code>. |
||
144 | * @see Database::delete() |
||
145 | */ |
||
146 | public function delete($id, array $condition = null) |
||
147 | { |
||
148 | if (!is_null($id)) |
||
149 | { |
||
150 | $condition = array($this->_id => $id); |
||
151 | } |
||
152 | else if (is_null($condition)) |
||
153 | { |
||
154 | throw new InvalidArgumentException( |
||
155 | '$id and $condition cannot both be null'); |
||
156 | } |
||
157 | |||
158 | return $this->_database->delete($this->_name, $condition); |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * Inserts a row into the table or updates an existing one |
||
163 | * |
||
164 | * @param array $data |
||
165 | * Associative array of column-value pairs to be updated/inserted |
||
166 | * @param string|array $condition |
||
167 | * If there are no records matching this condition, a row will be inserted; |
||
168 | * otherwise matching records are updated |
||
169 | * @return bool |
||
170 | * @see Table::update() |
||
171 | * @see Table::insert() |
||
172 | */ |
||
173 | public function updateOrInsert($data, array $condition = null) |
||
174 | { |
||
175 | if ($this->select($this->_id, $condition)) |
||
176 | { |
||
177 | return $this->update($data, $condition); |
||
178 | } |
||
179 | |||
180 | return $this->insert($data); |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Finds a record by ID |
||
185 | * |
||
186 | * @param mixed $id |
||
187 | */ |
||
188 | public function find($id) |
||
189 | { |
||
190 | /* DEBUG */ |
||
191 | if (defined('DEBUG') && DEBUG > 0) |
||
192 | { |
||
193 | debug($id); |
||
194 | } |
||
195 | |||
196 | return $this->select(null, array($this->_id => $id)); |
||
197 | } |
||
198 | } |