3 * Abstract base class for representing objects that are stored in some DB table.
4 * This is basically an ORM-like wrapper around rows in database tables that
5 * aims to be both simple and very flexible. It is centered around an associative
6 * array of fields and various methods to do common interaction with the database.
8 * Documentation inline and at https://www.mediawiki.org/wiki/Manual:ORMTable
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
30 * @license GNU GPL v2 or later
31 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
34 class ORMRow
implements IORMRow
{
37 * The fields of the object.
38 * field name (w/o prefix) => value
43 protected $fields = array( 'id' => null );
46 * If the object should update summaries of linked items when changed.
47 * For example, update the course_count field in universities when a course in courses is deleted.
48 * Settings this to false can prevent needless updating work in situations
49 * such as deleting a university, which will then delete all it's courses.
51 * @deprecated since 1.22
55 protected $updateSummaries = true;
58 * Indicates if the object is in summary mode.
59 * This mode indicates that only summary fields got updated,
60 * which allows for optimizations.
62 * @deprecated since 1.22
66 protected $inSummaryMode = false;
69 * @deprecated since 1.22
80 * @param IORMTable|null $table Deprecated since 1.22
81 * @param array|null $fields
82 * @param boolean $loadDefaults Deprecated since 1.22
84 public function __construct( IORMTable
$table = null, $fields = null, $loadDefaults = false ) {
85 $this->table
= $table;
87 if ( !is_array( $fields ) ) {
91 if ( $loadDefaults ) {
92 $fields = array_merge( $this->table
->getDefaults(), $fields );
95 $this->setFields( $fields );
99 * Load the specified fields from the database.
102 * @deprecated since 1.22
104 * @param array|null $fields
105 * @param boolean $override
106 * @param boolean $skipLoaded
108 * @return bool Success indicator
110 public function loadFields( $fields = null, $override = true, $skipLoaded = false ) {
111 if ( is_null( $this->getId() ) ) {
115 if ( is_null( $fields ) ) {
116 $fields = array_keys( $this->table
->getFields() );
120 $fields = array_diff( $fields, array_keys( $this->fields
) );
123 if ( !empty( $fields ) ) {
124 $result = $this->table
->rawSelectRow(
125 $this->table
->getPrefixedFields( $fields ),
126 array( $this->table
->getPrefixedField( 'id' ) => $this->getId() ),
127 array( 'LIMIT' => 1 ),
131 if ( $result !== false ) {
132 $this->setFields( $this->table
->getFieldsFromDBResult( $result ), $override );
142 * Gets the value of a field.
146 * @param string $name Field name
147 * @param $default mixed: Default value to return when none is found
150 * @throws MWException
153 public function getField( $name, $default = null ) {
154 if ( $this->hasField( $name ) ) {
155 return $this->fields
[$name];
156 } elseif ( !is_null( $default ) ) {
159 throw new MWException( 'Attempted to get not-set field ' . $name );
164 * Gets the value of a field but first loads it if not done so already.
167 * @deprecated since 1.22
169 * @param $name string
173 public function loadAndGetField( $name ) {
174 if ( !$this->hasField( $name ) ) {
175 $this->loadFields( array( $name ) );
178 return $this->getField( $name );
186 * @param string $name
188 public function removeField( $name ) {
189 unset( $this->fields
[$name] );
193 * Returns the objects database id.
197 * @return integer|null
199 public function getId() {
200 return $this->getField( 'id' );
204 * Sets the objects database id.
208 * @param integer|null $id
210 public function setId( $id ) {
211 $this->setField( 'id', $id );
215 * Gets if a certain field is set.
219 * @param string $name
223 public function hasField( $name ) {
224 return array_key_exists( $name, $this->fields
);
228 * Gets if the id field is set.
234 public function hasIdField() {
235 return $this->hasField( 'id' )
236 && !is_null( $this->getField( 'id' ) );
240 * Gets the fields => values to write to the table.
243 * @deprecated since 1.22
247 protected function getWriteValues() {
250 foreach ( $this->table
->getFields() as $name => $type ) {
251 if ( array_key_exists( $name, $this->fields
) ) {
252 $value = $this->fields
[$name];
254 // Skip null id fields so that the DBMS can set the default.
255 if ( $name === 'id' && is_null ( $value ) ) {
261 $value = (array)$value;
264 $value = serialize( $value );
268 $values[$this->table
->getPrefixedField( $name )] = $value;
276 * Sets multiple fields.
280 * @param array $fields The fields to set
281 * @param boolean $override Override already set fields with the provided values?
283 public function setFields( array $fields, $override = true ) {
284 foreach ( $fields as $name => $value ) {
285 if ( $override ||
!$this->hasField( $name ) ) {
286 $this->setField( $name, $value );
292 * Serializes the object to an associative array which
293 * can then easily be converted into JSON or similar.
297 * @param null|array $fields
298 * @param boolean $incNullId
302 public function toArray( $fields = null, $incNullId = false ) {
304 $setFields = array();
306 if ( !is_array( $fields ) ) {
307 $setFields = $this->getSetFieldNames();
309 foreach ( $fields as $field ) {
310 if ( $this->hasField( $field ) ) {
311 $setFields[] = $field;
316 foreach ( $setFields as $field ) {
317 if ( $incNullId ||
$field != 'id' ||
$this->hasIdField() ) {
318 $data[$field] = $this->getField( $field );
326 * Load the default values, via getDefaults.
329 * @deprecated since 1.22
331 * @param boolean $override
333 public function loadDefaults( $override = true ) {
334 $this->setFields( $this->table
->getDefaults(), $override );
338 * Writes the answer to the database, either updating it
339 * when it already exists, or inserting it when it doesn't.
342 * @deprecated since 1.22 Use IORMTable->updateRow or ->insertRow
344 * @param string|null $functionName
346 * @return boolean Success indicator
348 public function save( $functionName = null ) {
349 if ( $this->hasIdField() ) {
350 return $this->table
->updateRow( $this, $functionName );
352 return $this->table
->insertRow( $this, $functionName );
357 * Updates the object in the database.
360 * @deprecated since 1.22
362 * @param string|null $functionName
364 * @return boolean Success indicator
366 protected function saveExisting( $functionName = null ) {
367 $dbw = $this->table
->getWriteDbConnection();
369 $success = $dbw->update(
370 $this->table
->getName(),
371 $this->getWriteValues(),
372 $this->table
->getPrefixedValues( $this->getUpdateConditions() ),
373 is_null( $functionName ) ? __METHOD__
: $functionName
376 $this->table
->releaseConnection( $dbw );
378 // DatabaseBase::update does not always return true for success as documented...
379 return $success !== false;
383 * Returns the WHERE considtions needed to identify this object so
390 protected function getUpdateConditions() {
391 return array( 'id' => $this->getId() );
395 * Inserts the object into the database.
398 * @deprecated since 1.22
400 * @param string|null $functionName
401 * @param array|null $options
403 * @return boolean Success indicator
405 protected function insert( $functionName = null, array $options = null ) {
406 $dbw = $this->table
->getWriteDbConnection();
408 $success = $dbw->insert(
409 $this->table
->getName(),
410 $this->getWriteValues(),
411 is_null( $functionName ) ? __METHOD__
: $functionName,
415 // DatabaseBase::insert does not always return true for success as documented...
416 $success = $success !== false;
419 $this->setField( 'id', $dbw->insertId() );
422 $this->table
->releaseConnection( $dbw );
428 * Removes the object from the database.
431 * @deprecated since 1.22, use IORMTable->removeRow
433 * @return boolean Success indicator
435 public function remove() {
436 $this->beforeRemove();
438 $success = $this->table
->removeRow( $this, __METHOD__
);
448 * Gets called before an object is removed from the database.
451 * @deprecated since 1.22
453 protected function beforeRemove() {
454 $this->loadFields( $this->getBeforeRemoveFields(), false, true );
458 * Before removal of an object happens, @see beforeRemove gets called.
459 * This method loads the fields of which the names have been returned by this one (or all fields if null is returned).
460 * This allows for loading info needed after removal to get rid of linked data and the like.
466 protected function getBeforeRemoveFields() {
471 * Gets called after successful removal.
472 * Can be overridden to get rid of linked data.
475 * @deprecated since 1.22
477 protected function onRemoved() {
478 $this->setField( 'id', null );
482 * Return the names and values of the fields.
488 public function getFields() {
489 return $this->fields
;
493 * Return the names of the fields.
499 public function getSetFieldNames() {
500 return array_keys( $this->fields
);
504 * Sets the value of a field.
505 * Strings can be provided for other types,
506 * so this method can be called from unserialization handlers.
510 * @param string $name
511 * @param mixed $value
513 * @throws MWException
515 public function setField( $name, $value ) {
516 $this->fields
[$name] = $value;
520 * Add an amount (can be negative) to the specified field (needs to be numeric).
523 * @deprecated since 1.22, use IORMTable->addToField
525 * @param string $field
526 * @param integer $amount
528 * @return boolean Success indicator
530 public function addToField( $field, $amount ) {
531 return $this->table
->addToField( $this->getUpdateConditions(), $field, $amount );
535 * Return the names of the fields.
538 * @deprecated since 1.22
542 public function getFieldNames() {
543 return array_keys( $this->table
->getFields() );
547 * Computes and updates the values of the summary fields.
550 * @deprecated since 1.22
552 * @param array|string|null $summaryFields
554 public function loadSummaryFields( $summaryFields = null ) {
559 * Sets the value for the @see $updateSummaries field.
562 * @deprecated since 1.22
564 * @param boolean $update
566 public function setUpdateSummaries( $update ) {
567 $this->updateSummaries
= $update;
571 * Sets the value for the @see $inSummaryMode field.
574 * @deprecated since 1.22
576 * @param boolean $summaryMode
578 public function setSummaryMode( $summaryMode ) {
579 $this->inSummaryMode
= $summaryMode;
583 * Returns the table this IORMRow is a row in.
586 * @deprecated since 1.22
590 public function getTable() {