Subversion Repositories PHPX

Rev

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