Subversion Repositories PHPX

Rev

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