MediaWiki  REL1_24
ORMRow.php
Go to the documentation of this file.
00001 <?php
00034 class ORMRow implements IORMRow {
00042     protected $fields = array( 'id' => null );
00043 
00054     protected $updateSummaries = true;
00055 
00065     protected $inSummaryMode = false;
00066 
00072     protected $table;
00073 
00083     public function __construct( IORMTable $table = null, $fields = null, $loadDefaults = false ) {
00084         $this->table = $table;
00085 
00086         if ( !is_array( $fields ) ) {
00087             $fields = array();
00088         }
00089 
00090         if ( $loadDefaults ) {
00091             $fields = array_merge( $this->table->getDefaults(), $fields );
00092         }
00093 
00094         $this->setFields( $fields );
00095     }
00096 
00109     public function loadFields( $fields = null, $override = true, $skipLoaded = false ) {
00110         if ( is_null( $this->getId() ) ) {
00111             return false;
00112         }
00113 
00114         if ( is_null( $fields ) ) {
00115             $fields = array_keys( $this->table->getFields() );
00116         }
00117 
00118         if ( $skipLoaded ) {
00119             $fields = array_diff( $fields, array_keys( $this->fields ) );
00120         }
00121 
00122         if ( !empty( $fields ) ) {
00123             $result = $this->table->rawSelectRow(
00124                 $this->table->getPrefixedFields( $fields ),
00125                 array( $this->table->getPrefixedField( 'id' ) => $this->getId() ),
00126                 array( 'LIMIT' => 1 ),
00127                 __METHOD__
00128             );
00129 
00130             if ( $result !== false ) {
00131                 $this->setFields( $this->table->getFieldsFromDBResult( $result ), $override );
00132 
00133                 return true;
00134             }
00135 
00136             return false;
00137         }
00138 
00139         return true;
00140     }
00141 
00154     public function getField( $name, $default = null ) {
00155         if ( $this->hasField( $name ) ) {
00156             return $this->fields[$name];
00157         } elseif ( !is_null( $default ) ) {
00158             return $default;
00159         } else {
00160             throw new MWException( 'Attempted to get not-set field ' . $name );
00161         }
00162     }
00163 
00174     public function loadAndGetField( $name ) {
00175         if ( !$this->hasField( $name ) ) {
00176             $this->loadFields( array( $name ) );
00177         }
00178 
00179         return $this->getField( $name );
00180     }
00181 
00189     public function removeField( $name ) {
00190         unset( $this->fields[$name] );
00191     }
00192 
00200     public function getId() {
00201         return $this->getField( 'id' );
00202     }
00203 
00211     public function setId( $id ) {
00212         $this->setField( 'id', $id );
00213     }
00214 
00224     public function hasField( $name ) {
00225         return array_key_exists( $name, $this->fields );
00226     }
00227 
00235     public function hasIdField() {
00236         return $this->hasField( 'id' ) && !is_null( $this->getField( 'id' ) );
00237     }
00238 
00247     protected function getWriteValues() {
00248         $values = array();
00249 
00250         foreach ( $this->table->getFields() as $name => $type ) {
00251             if ( array_key_exists( $name, $this->fields ) ) {
00252                 $value = $this->fields[$name];
00253 
00254                 // Skip null id fields so that the DBMS can set the default.
00255                 if ( $name === 'id' && is_null( $value ) ) {
00256                     continue;
00257                 }
00258 
00259                 switch ( $type ) {
00260                     case 'array':
00261                         $value = (array)$value;
00262                     // fall-through!
00263                     case 'blob':
00264                         $value = serialize( $value );
00265                     // fall-through!
00266                 }
00267 
00268                 $values[$this->table->getPrefixedField( $name )] = $value;
00269             }
00270         }
00271 
00272         return $values;
00273     }
00274 
00283     public function setFields( array $fields, $override = true ) {
00284         foreach ( $fields as $name => $value ) {
00285             if ( $override || !$this->hasField( $name ) ) {
00286                 $this->setField( $name, $value );
00287             }
00288         }
00289     }
00290 
00302     public function toArray( $fields = null, $incNullId = false ) {
00303         $data = array();
00304         $setFields = array();
00305 
00306         if ( !is_array( $fields ) ) {
00307             $setFields = $this->getSetFieldNames();
00308         } else {
00309             foreach ( $fields as $field ) {
00310                 if ( $this->hasField( $field ) ) {
00311                     $setFields[] = $field;
00312                 }
00313             }
00314         }
00315 
00316         foreach ( $setFields as $field ) {
00317             if ( $incNullId || $field != 'id' || $this->hasIdField() ) {
00318                 $data[$field] = $this->getField( $field );
00319             }
00320         }
00321 
00322         return $data;
00323     }
00324 
00333     public function loadDefaults( $override = true ) {
00334         $this->setFields( $this->table->getDefaults(), $override );
00335     }
00336 
00348     public function save( $functionName = null ) {
00349         if ( $this->hasIdField() ) {
00350             return $this->table->updateRow( $this, $functionName );
00351         } else {
00352             return $this->table->insertRow( $this, $functionName );
00353         }
00354     }
00355 
00366     protected function saveExisting( $functionName = null ) {
00367         $dbw = $this->table->getWriteDbConnection();
00368 
00369         $success = $dbw->update(
00370             $this->table->getName(),
00371             $this->getWriteValues(),
00372             $this->table->getPrefixedValues( $this->getUpdateConditions() ),
00373             is_null( $functionName ) ? __METHOD__ : $functionName
00374         );
00375 
00376         $this->table->releaseConnection( $dbw );
00377 
00378         // DatabaseBase::update does not always return true for success as documented...
00379         return $success !== false;
00380     }
00381 
00390     protected function getUpdateConditions() {
00391         return array( 'id' => $this->getId() );
00392     }
00393 
00405     protected function insert( $functionName = null, array $options = null ) {
00406         $dbw = $this->table->getWriteDbConnection();
00407 
00408         $success = $dbw->insert(
00409             $this->table->getName(),
00410             $this->getWriteValues(),
00411             is_null( $functionName ) ? __METHOD__ : $functionName,
00412             $options
00413         );
00414 
00415         // DatabaseBase::insert does not always return true for success as documented...
00416         $success = $success !== false;
00417 
00418         if ( $success ) {
00419             $this->setField( 'id', $dbw->insertId() );
00420         }
00421 
00422         $this->table->releaseConnection( $dbw );
00423 
00424         return $success;
00425     }
00426 
00435     public function remove() {
00436         $this->beforeRemove();
00437 
00438         $success = $this->table->removeRow( $this, __METHOD__ );
00439 
00440         if ( $success ) {
00441             $this->onRemoved();
00442         }
00443 
00444         return $success;
00445     }
00446 
00453     protected function beforeRemove() {
00454         $this->loadFields( $this->getBeforeRemoveFields(), false, true );
00455     }
00456 
00467     protected function getBeforeRemoveFields() {
00468         return array();
00469     }
00470 
00478     protected function onRemoved() {
00479         $this->setField( 'id', null );
00480     }
00481 
00489     public function getFields() {
00490         return $this->fields;
00491     }
00492 
00500     public function getSetFieldNames() {
00501         return array_keys( $this->fields );
00502     }
00503 
00516     public function setField( $name, $value ) {
00517         $this->fields[$name] = $value;
00518     }
00519 
00531     public function addToField( $field, $amount ) {
00532         return $this->table->addToField( $this->getUpdateConditions(), $field, $amount );
00533     }
00534 
00543     public function getFieldNames() {
00544         return array_keys( $this->table->getFields() );
00545     }
00546 
00555     public function loadSummaryFields( $summaryFields = null ) {
00556     }
00557 
00566     public function setUpdateSummaries( $update ) {
00567         $this->updateSummaries = $update;
00568     }
00569 
00578     public function setSummaryMode( $summaryMode ) {
00579         $this->inSummaryMode = $summaryMode;
00580     }
00581 
00590     public function getTable() {
00591         return $this->table;
00592     }
00593 }