Subversion Repositories PHPX

Rev

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

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