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