Subversion Repositories PHPX

Rev

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

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