Subversion Repositories PHPX

Rev

Rev 50 | Rev 52 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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