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