Updated whitespaces
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / data / DatabaseObjectEditor.class.php
1 <?php
2 namespace wcf\data;
3 use wcf\system\WCF;
4
5 /**
6 * Basic implementation for object editors following the decorator pattern.
7 *
8 * @author Marcel Werk
9 * @copyright 2001-2011 WoltLab GmbH
10 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
11 * @package com.woltlab.wcf
12 * @subpackage data
13 * @category Community Framework
14 */
15 abstract class DatabaseObjectEditor extends DatabaseObjectDecorator implements IEditableObject {
16 /**
17 * @see wcf\data\IEditableObject::create()
18 */
19 public static function create(array $parameters = array()) {
20 $keys = $values = '';
21 $statementParameters = array();
22 foreach ($parameters as $key => $value) {
23 if (!empty($keys)) {
24 $keys .= ',';
25 $values .= ',';
26 }
27
28 $keys .= $key;
29 $values .= '?';
30 $statementParameters[] = $value;
31 }
32
33 // save object
34 $sql = "INSERT INTO ".static::getDatabaseTableName()."
35 (".$keys.")
36 VALUES (".$values.")";
37 $statement = WCF::getDB()->prepareStatement($sql);
38 $statement->execute($statementParameters);
39
40 // return new object
41 if (static::getDatabaseTableIndexIsIdentity()) {
42 $id = WCF::getDB()->getInsertID(static::getDatabaseTableName(), static::getDatabaseTableIndexName());
43 }
44 else {
45 $id = $parameters[static::getDatabaseTableIndexName()];
46 }
47 return new static::$baseClass($id);
48 }
49
50 /**
51 * @see wcf\data\IEditableObject::update()
52 */
53 public function update(array $parameters = array()) {
54 if (!count($parameters)) return;
55
56 // check wether any value changed or not
57 $update = false;
58 foreach ($parameters as $name => $value) {
59 if ($this->__get($name) != $value) {
60 $update = true;
61 break;
62 }
63 }
64
65 // there is no new data - break to avoid senseless sql queries
66 if (!$update) return;
67
68 $updateSQL = '';
69 $statementParameters = array();
70 foreach ($parameters as $key => $value) {
71 if (!empty($updateSQL)) $updateSQL .= ', ';
72 $updateSQL .= $key . ' = ?';
73 $statementParameters[] = $value;
74 }
75 $statementParameters[] = $this->__get(static::getDatabaseTableIndexName());
76
77 $sql = "UPDATE ".static::getDatabaseTableName()."
78 SET ".$updateSQL."
79 WHERE ".static::getDatabaseTableIndexName()." = ?";
80 $statement = WCF::getDB()->prepareStatement($sql);
81 $statement->execute($statementParameters);
82 }
83
84 /**
85 * @see wcf\data\IEditableObject::updateCounters()
86 */
87 public function updateCounters(array $counters = array()) {
88 if (!count($counters)) return;
89
90 $updateSQL = '';
91 $statementParameters = array();
92 foreach ($counters as $key => $value) {
93 if (!empty($updateSQL)) $updateSQL .= ', ';
94 $updateSQL .= $key . ' = ' . $key . ' + ?';
95 $statementParameters[] = $value;
96 }
97 $statementParameters[] = $this->__get(static::getDatabaseTableIndexName());
98
99 $sql = "UPDATE ".static::getDatabaseTableName()."
100 SET ".$updateSQL."
101 WHERE ".static::getDatabaseTableIndexName()." = ?";
102 $statement = WCF::getDB()->prepareStatement($sql);
103 $statement->execute($statementParameters);
104 }
105
106 /**
107 * @see wcf\data\IEditableObject::delete()
108 */
109 public function delete() {
110 static::deleteAll(array($this->__get(static::getDatabaseTableIndexName())));
111 }
112
113 /**
114 * @see wcf\data\IEditableObject::deleteAll()
115 */
116 public static function deleteAll(array $objectIDs = array()) {
117 $sql = "DELETE FROM ".static::getDatabaseTableName()."
118 WHERE ".static::getDatabaseTableIndexName()." = ?";
119 $statement = WCF::getDB()->prepareStatement($sql);
120
121 $affectedCount = 0;
122 WCF::getDB()->beginTransaction();
123 foreach ($objectIDs as $objectID) {
124 $statement->executeUnbuffered(array($objectID));
125 $affectedCount += $statement->getAffectedRows();
126 }
127 WCF::getDB()->commitTransaction();
128
129 return $affectedCount;
130 }
131 }