Rev 35 | Rev 51 | 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 | |||
| 34 | PointedEar | 3 | require_once __DIR__ . '/../Application.php'; |
| 29 | PointedEar | 4 | require_once __DIR__ . '/../AbstractModel.php'; |
| 27 | PointedEar | 5 | |
| 6 | /** |
||
| 7 | * Generic database table model class |
||
| 8 | * |
||
| 9 | * @author Thomas Lahn |
||
| 50 | PointedEar | 10 | * @property Database $database |
| 27 | PointedEar | 11 | * @property-read int $lastInsertId |
| 12 | * ID of the last inserted row, or the last value from |
||
| 13 | a sequence object, depending on the underlying driver. |
||
| 14 | */ |
||
| 34 | PointedEar | 15 | class Table extends AbstractModel |
| 27 | PointedEar | 16 | { |
| 17 | /** |
||
| 18 | * Name of the table |
||
| 19 | */ |
||
| 20 | protected $_name = ''; |
||
| 50 | PointedEar | 21 | |
| 27 | PointedEar | 22 | /** |
| 23 | * Database of the table |
||
| 50 | PointedEar | 24 | * @var Database|string |
| 27 | PointedEar | 25 | */ |
| 26 | protected $_database; |
||
| 50 | PointedEar | 27 | |
| 27 | PointedEar | 28 | protected $_id = 'id'; |
| 50 | PointedEar | 29 | |
| 34 | PointedEar | 30 | /** |
| 31 | * Creates a new <code>Table</code> instance. |
||
| 32 | * |
||
| 33 | * Each of the parameters is optional and can also be given |
||
| 34 | * by a protected property where the parameter name is preceded |
||
| 35 | * by <code>_</code>. Parameter values overwrite the default |
||
| 36 | * property values. It is recommended to use default property |
||
| 37 | * values of inheriting classes except for small applications |
||
| 38 | * and testing purposes. |
||
| 39 | * |
||
| 40 | * @param Database $database |
||
| 41 | * Database of the table (required in order to use a fitting |
||
| 42 | * query language) |
||
| 43 | * @param string $name |
||
| 44 | * Table name |
||
| 45 | * @param string $id |
||
| 46 | * Name of the primary key column |
||
| 47 | * @throws InvalidArgumentException |
||
| 48 | */ |
||
| 50 | PointedEar | 49 | public function __construct($database = null, $name = '', $id = '') |
| 27 | PointedEar | 50 | { |
| 34 | PointedEar | 51 | if ($database === null) |
| 52 | { |
||
| 50 | PointedEar | 53 | /* Call getter to convert to Database if possible */ |
| 54 | if ($this->database === null) |
||
| 55 | { |
||
| 56 | $this->_database = Application::getInstance()->getDefaultDatabase(); |
||
| 57 | } |
||
| 34 | PointedEar | 58 | } |
| 59 | else |
||
| 60 | { |
||
| 61 | $this->_database = $database; |
||
| 62 | } |
||
| 50 | PointedEar | 63 | |
| 34 | PointedEar | 64 | if ($name !== '') |
| 65 | { |
||
| 66 | $this->_name = $name; |
||
| 67 | } |
||
| 50 | PointedEar | 68 | |
| 69 | if (!\is_string($this->_name)) |
||
| 34 | PointedEar | 70 | { |
| 50 | PointedEar | 71 | throw new \InvalidArgumentException( |
| 72 | 'Expected string for table name, saw ' |
||
| 73 | . \get_class($this->_name) || \gettype($this->_name)); |
||
| 34 | PointedEar | 74 | } |
| 75 | |||
| 76 | if ($id !== '') |
||
| 77 | { |
||
| 78 | $this->_id = $id; |
||
| 79 | } |
||
| 27 | PointedEar | 80 | } |
| 50 | PointedEar | 81 | |
| 27 | PointedEar | 82 | /** |
| 83 | * Returns the database for the table |
||
| 84 | * @return Database |
||
| 85 | */ |
||
| 86 | public function getDatabase() |
||
| 87 | { |
||
| 50 | PointedEar | 88 | if (\is_string($this->_database)) |
| 89 | { |
||
| 90 | /* Call setter to convert to Database */ |
||
| 91 | $this->database = $this->_database; |
||
| 92 | } |
||
| 93 | |||
| 27 | PointedEar | 94 | return $this->_database; |
| 95 | } |
||
| 50 | PointedEar | 96 | |
| 27 | PointedEar | 97 | /** |
| 50 | PointedEar | 98 | * @param Database|string $value |
| 99 | * @throws InvalidArgumentException |
||
| 100 | */ |
||
| 101 | public function setDatabase ($value) |
||
| 102 | { |
||
| 103 | if ($value instanceof Database) |
||
| 104 | { |
||
| 105 | $this->_database = $value; |
||
| 106 | } |
||
| 107 | else if ($value !== null) |
||
| 108 | { |
||
| 109 | $database = new $value(); |
||
| 110 | if (!($database instanceof Database)) |
||
| 111 | { |
||
| 112 | throw new \InvalidArgumentException( |
||
| 113 | 'Expected Database instance or string for class name, saw ' |
||
| 114 | . (\get_class($value) || \gettype($value)) |
||
| 115 | ); |
||
| 116 | } |
||
| 117 | |||
| 118 | $this->_database = $database; |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 27 | PointedEar | 123 | * Initiates a transaction |
| 124 | * |
||
| 125 | * @return bool |
||
| 126 | * @see Database::beginTransaction() |
||
| 127 | */ |
||
| 128 | public function beginTransaction() |
||
| 129 | { |
||
| 130 | return $this->_database->beginTransaction(); |
||
| 131 | } |
||
| 50 | PointedEar | 132 | |
| 27 | PointedEar | 133 | /** |
| 134 | * Rolls back a transaction |
||
| 135 | * |
||
| 136 | * @return bool |
||
| 137 | * @see Database::rollBack() |
||
| 138 | */ |
||
| 139 | public function rollBack() |
||
| 140 | { |
||
| 141 | return $this->_database->rollBack(); |
||
| 142 | } |
||
| 50 | PointedEar | 143 | |
| 27 | PointedEar | 144 | /** |
| 145 | * Commits a transaction |
||
| 146 | * |
||
| 147 | * @return bool |
||
| 148 | * @see Database::commit() |
||
| 149 | */ |
||
| 150 | public function commit() |
||
| 151 | { |
||
| 152 | return $this->_database->commit(); |
||
| 153 | } |
||
| 50 | PointedEar | 154 | |
| 27 | PointedEar | 155 | /** |
| 156 | * Retrieves all rows from the table |
||
| 157 | * |
||
| 158 | * @return array |
||
| 159 | * @see Database::fetchAll() |
||
| 160 | */ |
||
| 161 | public function fetchAll($fetch_style = null, $column_index = null, array $ctor_args = null) |
||
| 162 | { |
||
| 163 | return $this->_database->fetchAll($this->_name, $fetch_style, $column_index, $ctor_args); |
||
| 164 | } |
||
| 50 | PointedEar | 165 | |
| 27 | PointedEar | 166 | /** |
| 167 | * Selects data from one or more tables |
||
| 168 | * |
||
| 169 | * @return array |
||
| 170 | * @see Database::select() |
||
| 171 | */ |
||
| 172 | public function select($columns = null, $where = null, $order = null, $limit = null) |
||
| 173 | { |
||
| 174 | return $this->_database->select($this->_name, $columns, $where, $order, $limit); |
||
| 175 | } |
||
| 50 | PointedEar | 176 | |
| 27 | PointedEar | 177 | /** |
| 178 | * Updates records in one or more tables |
||
| 179 | * |
||
| 180 | * @return bool |
||
| 181 | * @see Database::update() |
||
| 182 | */ |
||
| 183 | public function update($data, $condition) |
||
| 184 | { |
||
| 185 | return $this->_database->update($this->_name, $data, $condition); |
||
| 186 | } |
||
| 50 | PointedEar | 187 | |
| 27 | PointedEar | 188 | /** |
| 189 | * Inserts a record into the table |
||
| 190 | * |
||
| 191 | * @return bool |
||
| 192 | * @see Database::insert() |
||
| 193 | */ |
||
| 194 | public function insert($data, $cols = null) |
||
| 195 | { |
||
| 196 | return $this->_database->insert($this->_name, $data, $cols); |
||
| 197 | } |
||
| 50 | PointedEar | 198 | |
| 27 | PointedEar | 199 | /** |
| 200 | * Returns the ID of the last inserted row, or the last value from |
||
| 201 | * a sequence object, depending on the underlying driver. |
||
| 202 | * |
||
| 203 | * @return int |
||
| 204 | * @see Database::getLastInsertId() |
||
| 205 | */ |
||
| 206 | public function getLastInsertId() |
||
| 207 | { |
||
| 208 | return $this->_database->lastInsertId; |
||
| 209 | } |
||
| 50 | PointedEar | 210 | |
| 27 | PointedEar | 211 | /** |
| 212 | * Delete a record from the table |
||
| 213 | * |
||
| 214 | * @param int $id |
||
| 215 | * ID of the record to delete. May be <code>null</code>, |
||
| 216 | * in which case <var>$condition</var> must specify |
||
| 217 | * the records to be deleted. |
||
| 218 | * @param array[optional] $condition |
||
| 219 | * Conditions that must be met for a record to be deleted. |
||
| 220 | * Ignored if <var>$id</var> is not <code>null</code>. |
||
| 221 | * @return bool |
||
| 222 | * @throws InvalidArgumentException if both <var>$id</var> and |
||
| 223 | * <var>$condition</var> are <code>null</code>. |
||
| 224 | * @see Database::delete() |
||
| 225 | */ |
||
| 50 | PointedEar | 226 | public function delete ($id, array $condition = null) |
| 27 | PointedEar | 227 | { |
| 50 | PointedEar | 228 | if ($id !== null) |
| 27 | PointedEar | 229 | { |
| 230 | $condition = array($this->_id => $id); |
||
| 231 | } |
||
| 50 | PointedEar | 232 | else if ($condition === null) |
| 27 | PointedEar | 233 | { |
| 234 | throw new InvalidArgumentException( |
||
| 235 | '$id and $condition cannot both be null'); |
||
| 236 | } |
||
| 50 | PointedEar | 237 | |
| 27 | PointedEar | 238 | return $this->_database->delete($this->_name, $condition); |
| 239 | } |
||
| 50 | PointedEar | 240 | |
| 27 | PointedEar | 241 | /** |
| 242 | * Inserts a row into the table or updates an existing one |
||
| 243 | * |
||
| 244 | * @param array $data |
||
| 245 | * Associative array of column-value pairs to be updated/inserted |
||
| 246 | * @param string|array $condition |
||
| 247 | * If there are no records matching this condition, a row will be inserted; |
||
| 248 | * otherwise matching records are updated |
||
| 249 | * @return bool |
||
| 250 | * @see Table::update() |
||
| 251 | * @see Table::insert() |
||
| 252 | */ |
||
| 253 | public function updateOrInsert($data, array $condition = null) |
||
| 254 | { |
||
| 255 | if ($this->select($this->_id, $condition)) |
||
| 256 | { |
||
| 257 | return $this->update($data, $condition); |
||
| 258 | } |
||
| 259 | |||
| 50 | PointedEar | 260 | return $this->insert($data); |
| 27 | PointedEar | 261 | } |
| 262 | |||
| 263 | /** |
||
| 264 | * Finds a record by ID |
||
| 265 | * |
||
| 266 | * @param mixed $id |
||
| 35 | PointedEar | 267 | * @return array |
| 27 | PointedEar | 268 | */ |
| 50 | PointedEar | 269 | public function find ($id) |
| 27 | PointedEar | 270 | { |
| 271 | /* DEBUG */ |
||
| 272 | if (defined('DEBUG') && DEBUG > 0) |
||
| 273 | { |
||
| 274 | debug($id); |
||
| 275 | } |
||
| 50 | PointedEar | 276 | |
| 35 | PointedEar | 277 | $result = $this->select(null, array($this->_id => $id)); |
| 50 | PointedEar | 278 | |
| 35 | PointedEar | 279 | if ($result) |
| 280 | { |
||
| 281 | $result = $result[0]; |
||
| 282 | } |
||
| 50 | PointedEar | 283 | |
| 35 | PointedEar | 284 | return $result; |
| 27 | PointedEar | 285 | } |
| 286 | } |