6 * Abstract class for all data holder classes.
9 * @copyright 2001-2014 WoltLab GmbH
10 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
11 * @package com.woltlab.wcf
13 * @category Community Framework
15 abstract class DatabaseObject
implements IStorableObject
{
17 * database table for this object
20 protected static $databaseTableName = '';
23 * indicates if database table index is an identity column
26 protected static $databaseTableIndexIsIdentity = true;
29 * name of the primary index column
32 protected static $databaseTableIndexName = '';
38 protected static $sortBy = null;
44 protected static $sortOrder = null;
50 protected $data = null;
53 * Creates a new instance of the DatabaseObject class.
57 * @param \wcf\data\DatabaseObject $object
59 public function __construct($id, array $row = null, DatabaseObject
$object = null) {
62 FROM ".static::getDatabaseTableName()."
63 WHERE ".static::getDatabaseTableIndexName()." = ?";
64 $statement = WCF
::getDB()->prepareStatement($sql);
65 $statement->execute(array($id));
66 $row = $statement->fetchArray();
68 // enforce data type 'array'
69 if ($row === false) $row = array();
71 else if ($object !== null) {
75 $this->handleData($row);
79 * Stores the data of a database row.
83 protected function handleData($data) {
84 // provide a logical false value for - assumed numeric - primary index
85 if (!isset($data[static::getDatabaseTableIndexName()])) {
86 $data[static::getDatabaseTableIndexName()] = 0;
93 * @see \wcf\data\IStorableObject::__get()
95 public function __get($name) {
96 if (isset($this->data
[$name])) {
97 return $this->data
[$name];
105 * Returns the id of the object.
109 public function getObjectID() {
110 return $this->data
[static::getDatabaseTableIndexName()];
114 * @see \wcf\data\IStorableObject::__isset()
116 public function __isset($name) {
117 return isset($this->data
[$name]);
121 * @see \wcf\data\IStorableObject::getData()
123 public function getData() {
128 * @see \wcf\data\IStorableObject::getDatabaseTableName()
130 public static function getDatabaseTableName() {
131 return 'wcf'.WCF_N
.'_'.static::$databaseTableName;
135 * @see \wcf\data\IStorableObject::getDatabaseTableAlias()
137 public static function getDatabaseTableAlias() {
138 return static::$databaseTableName;
142 * @see \wcf\data\IStorableObject::getDatabaseTableIndexIsIdentity()
144 public static function getDatabaseTableIndexIsIdentity() {
145 return static::$databaseTableIndexIsIdentity;
149 * @see \wcf\data\IStorableObject::getDatabaseTableIndexName()
151 public static function getDatabaseTableIndexName() {
152 return static::$databaseTableIndexName;
156 * Sorts a list of database objects.
158 * @param array<\wcf\data\DatabaseObject> $objects
159 * @param mixed $sortBy
160 * @param string $sortOrder
163 public static function sort(&$objects, $sortBy, $sortOrder = 'ASC', $maintainIndexAssociation = true) {
164 $sortArray = $objects2 = array();
165 foreach ($objects as $idx => $obj) {
166 $sortArray[$idx] = $obj->$sortBy;
168 // array_multisort will drop index association if key is not a string
169 if ($maintainIndexAssociation) {
170 $objects2[$idx.'x'] = $obj;
174 if ($maintainIndexAssociation) {
176 array_multisort($sortArray, $sortOrder == 'ASC' ? SORT_ASC
: SORT_DESC
, $objects2);
179 foreach ($objects2 as $idx => $obj) {
180 $objects[substr($idx, 0, -1)] = $obj;
184 array_multisort($sortArray, $sortOrder == 'ASC' ? SORT_ASC
: SORT_DESC
, $objects);