From 41f69863da2895b952052720234ffc8faf2a7324 Mon Sep 17 00:00:00 2001 From: Jeroen De Dauw Date: Tue, 14 Feb 2012 21:16:39 +0000 Subject: [PATCH] follow up to r111468 - moved a bunch of methods from DBDataObject to DBTable and some minor changes --- includes/DBDataObject.php | 53 ++------------------------------- includes/DBTable.php | 62 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 62 insertions(+), 53 deletions(-) diff --git a/includes/DBDataObject.php b/includes/DBDataObject.php index 2059715de2..1e5bc26212 100644 --- a/includes/DBDataObject.php +++ b/includes/DBDataObject.php @@ -94,6 +94,7 @@ abstract class DBDataObject { * * @since 1.20 * + * @param DBTable $table * @param array|null $fields * @param boolean $loadDefaults */ @@ -528,20 +529,6 @@ abstract class DBDataObject { } } - /** - * Get a new instance of the class from an array. - * - * @since 1.20 - * - * @param array $data - * @param boolean $loadDefaults - * - * @return DBDataObject - */ - public function newFromArray( array $data, $loadDefaults = false ) { - return new self( $data, $loadDefaults ); - } - /** * Get the database type used for read operations. * @@ -706,43 +693,7 @@ abstract class DBDataObject { return false; } - - protected function getDBTable() { - return $this->table->getDBTable(); - } - - /** - * Get an array with fields from a database result, - * that can be fed directly to the constructor or - * to setFields. - * - * @since 1.20 - * - * @param object $result - * - * @return array - */ - public function getFieldsFromDBResult( $result ) { - $result = (array)$result; - return array_combine( - $this->unprefixFieldNames( array_keys( $result ) ), - array_values( $result ) - ); - } - - /** - * Get a new instance of the class from a database result. - * - * @since 1.20 - * - * @param stdClass $result - * - * @return DBDataObject - */ - public function newFromDBResult( stdClass $result ) { - return $this->newFromArray( $this->getFieldsFromDBResult( $result ) ); - } - + /** * Returns the table this DBDataObject is a row in. * diff --git a/includes/DBTable.php b/includes/DBTable.php index ad0923459f..0353a69232 100644 --- a/includes/DBTable.php +++ b/includes/DBTable.php @@ -29,7 +29,7 @@ abstract class DBTable { * * @return string */ - protected abstract function getDataObjectClass(); + public abstract function getDataObjectClass(); /** * Gets the db field prefix. @@ -482,7 +482,7 @@ abstract class DBTable { * @return array */ public function unprefixFieldNames( array $fieldNames ) { - return array_map( '$this->unprefixFieldName', $fieldNames ); + return array_map( array( $this, 'unprefixFieldName' ), $fieldNames ); } /** @@ -518,5 +518,63 @@ abstract class DBTable { return $instance; } + + /** + * Get an array with fields from a database result, + * that can be fed directly to the constructor or + * to setFields. + * + * @since 1.20 + * + * @param stdClass $result + * + * @return array + */ + public function getFieldsFromDBResult( stdClass $result ) { + $result = (array)$result; + return array_combine( + $this->unprefixFieldNames( array_keys( $result ) ), + array_values( $result ) + ); + } + + /** + * Get a new instance of the class from a database result. + * + * @since 1.20 + * + * @param stdClass $result + * + * @return DBDataObject + */ + public function newFromDBResult( stdClass $result ) { + return $this->newFromArray( $this->getFieldsFromDBResult( $result ) ); + } + + /** + * Get a new instance of the class from an array. + * + * @since 1.20 + * + * @param array $data + * @param boolean $loadDefaults + * + * @return DBDataObject + */ + public function newFromArray( array $data, $loadDefaults = false ) { + $class = $this->getDataObjectClass(); + return new $class( $this, $data, $loadDefaults ); + } + + /** + * Return the names of the fields. + * + * @since 1.20 + * + * @return array + */ + public function getFieldNames() { + return array_keys( $this->getFieldTypes() ); + } } -- 2.20.1