Made ORMTable and ORMRow non-abstract
authorjeroendedauw <jeroendedauw@gmail.com>
Fri, 18 Jan 2013 09:29:18 +0000 (10:29 +0100)
committerjeroendedauw <jeroendedauw@gmail.com>
Fri, 18 Jan 2013 09:29:20 +0000 (10:29 +0100)
ORMTable now optionally takes info such as table name and field definitions in its constructor

Change-Id: I9cee11fdb58d4ef57d442f69650c6640d767cfa1

includes/dao/DBAccessBase.php
includes/db/ORMRow.php
includes/db/ORMTable.php

index a27bef5..20cf5b4 100644 (file)
@@ -33,7 +33,7 @@ abstract class DBAccessBase implements IDBAccessObject {
         * @var String|bool $wiki The target wiki's name. This must be an ID
         * that LBFactory can understand.
         */
-       protected $wiki;
+       protected $wiki = false;
 
        /**
         * @param String|bool $wiki The target wiki's name. This must be an ID
index 5c730fb..6acc124 100644 (file)
@@ -31,7 +31,7 @@
  * @author Jeroen De Dauw < jeroendedauw@gmail.com >
  */
 
-abstract class ORMRow implements IORMRow {
+class ORMRow implements IORMRow {
 
        /**
         * The fields of the object.
index 4079315..8abfdb6 100644 (file)
@@ -19,6 +19,7 @@
  * http://www.gnu.org/copyleft/gpl.html
  *
  * @since 1.20
+ * Non-abstract since 1.21
  *
  * @file ORMTable.php
  * @ingroup ORM
  * @author Jeroen De Dauw < jeroendedauw@gmail.com >
  */
 
-abstract class ORMTable extends DBAccessBase implements IORMTable {
+class ORMTable extends DBAccessBase implements IORMTable {
 
        /**
-        * Gets the db field prefix.
+        * Cache for instances, used by the singleton method.
         *
         * @since 1.20
+        * @deprecated since 1.21
         *
-        * @return string
+        * @var ORMTable[]
         */
-       protected abstract function getFieldPrefix();
+       protected static $instanceCache = array();
 
        /**
-        * Cache for instances, used by the singleton method.
+        * @since 1.21
         *
-        * @since 1.20
-        * @var array of DBTable
+        * @var string
         */
-       protected static $instanceCache = array();
+       protected $tableName;
+
+       /**
+        * @since 1.21
+        *
+        * @var string[]
+        */
+       protected $fields = array();
+
+       /**
+        * @since 1.21
+        *
+        * @var string
+        */
+       protected $fieldPrefix = '';
+
+       /**
+        * @since 1.21
+        *
+        * @var string
+        */
+       protected $rowClass = 'ORMRow';
+
+       /**
+        * @since 1.21
+        *
+        * @var array
+        */
+       protected $defaults = array();
 
        /**
         * ID of the database connection to use for read operations.
         * Can be changed via @see setReadDb.
         *
         * @since 1.20
+        *
         * @var integer DB_ enum
         */
        protected $readDb = DB_SLAVE;
 
+       /**
+        * Constructor.
+        *
+        * @since 1.21
+        *
+        * @param string $tableName
+        * @param string[] $fields
+        * @param array $defaults
+        * @param string|null $rowClass
+        * @param string $fieldPrefix
+        */
+       public function __construct( $tableName = '', array $fields = array(), array $defaults = array(), $rowClass = null, $fieldPrefix = '' ) {
+               $this->tableName = $tableName;
+               $this->fields = $fields;
+               $this->defaults = $defaults;
+
+               if ( is_string( $rowClass ) ) {
+                       $this->rowClass = $rowClass;
+               }
+
+               $this->fieldPrefix = $fieldPrefix;
+       }
+
+       /**
+        * @see IORMTable::getName
+        *
+        * @since 1.21
+        *
+        * @return string
+        * @throws MWException
+        */
+       public function getName() {
+               if ( $this->tableName === '' ) {
+                       throw new MWException( 'The table name needs to be set' );
+               }
+
+               return $this->tableName;
+       }
+
+       /**
+        * Gets the db field prefix.
+        *
+        * @since 1.20
+        *
+        * @return string
+        */
+       protected function getFieldPrefix() {
+               return $this->fieldPrefix;
+       }
+
+       /**
+        * @see IORMTable::getRowClass
+        *
+        * @since 1.21
+        *
+        * @return string
+        */
+       public function getRowClass() {
+               return $this->rowClass;
+       }
+
+       /**
+        * @see ORMTable::getFields
+        *
+        * @since 1.21
+        *
+        * @return array
+        * @throws MWException
+        */
+       public function getFields() {
+               if ( $this->fields === array() ) {
+                       throw new MWException( 'The table needs to have one or more fields' );
+               }
+
+               return $this->fields;
+       }
+
        /**
         * Returns a list of default field values.
         * field name => field value
@@ -64,7 +171,7 @@ abstract class ORMTable extends DBAccessBase implements IORMTable {
         * @return array
         */
        public function getDefaults() {
-               return array();
+               return $this->defaults;
        }
 
        /**
@@ -680,6 +787,7 @@ abstract class ORMTable extends DBAccessBase implements IORMTable {
         * Get an instance of this class.
         *
         * @since 1.20
+        * @deprecated since 1.21
         *
         * @return IORMTable
         */
@@ -793,4 +901,4 @@ abstract class ORMTable extends DBAccessBase implements IORMTable {
                return array_key_exists( $name, $this->getFields() );
        }
 
-}
+}
\ No newline at end of file