Merge branch '3.0' into 3.1
[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-2018 WoltLab GmbH
10 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
11 * @package WoltLabSuite\Core\Data
12 */
13 abstract class DatabaseObjectEditor extends DatabaseObjectDecorator implements IEditableObject {
14 /**
15 * @inheritDoc
16 */
17 public static function create(array $parameters = []) {
18 $keys = $values = '';
19 $statementParameters = [];
20 foreach ($parameters as $key => $value) {
21 if (!empty($keys)) {
22 $keys .= ',';
23 $values .= ',';
24 }
25
26 $keys .= $key;
27 $values .= '?';
28 $statementParameters[] = $value;
29 }
30
31 // save object
32 $sql = "INSERT INTO ".static::getDatabaseTableName()."
33 (".$keys.")
34 VALUES (".$values.")";
35 $statement = WCF::getDB()->prepareStatement($sql);
36 $statement->execute($statementParameters);
37
38 // return new object
39 if (static::getDatabaseTableIndexIsIdentity()) {
40 $id = WCF::getDB()->getInsertID(static::getDatabaseTableName(), static::getDatabaseTableIndexName());
41 }
42 else {
43 $id = $parameters[static::getDatabaseTableIndexName()];
44 }
45 return new static::$baseClass($id);
46 }
47
48 /**
49 * @inheritDoc
50 */
51 public function update(array $parameters = []) {
52 if (empty($parameters)) return;
53
54 $updateSQL = '';
55 $statementParameters = [];
56 foreach ($parameters as $key => $value) {
57 if (!empty($updateSQL)) $updateSQL .= ', ';
58 $updateSQL .= $key . ' = ?';
59 $statementParameters[] = $value;
60 }
61 $statementParameters[] = $this->getObjectID();
62
63 $sql = "UPDATE ".static::getDatabaseTableName()."
64 SET ".$updateSQL."
65 WHERE ".static::getDatabaseTableIndexName()." = ?";
66 $statement = WCF::getDB()->prepareStatement($sql);
67 $statement->execute($statementParameters);
68 }
69
70 /**
71 * @inheritDoc
72 */
73 public function updateCounters(array $counters = []) {
74 if (empty($counters)) return;
75
76 $updateSQL = '';
77 $statementParameters = [];
78 foreach ($counters as $key => $value) {
79 if (!empty($updateSQL)) $updateSQL .= ', ';
80 $updateSQL .= $key . ' = ' . $key . ' + ?';
81 $statementParameters[] = $value;
82 }
83 $statementParameters[] = $this->getObjectID();
84
85 $sql = "UPDATE ".static::getDatabaseTableName()."
86 SET ".$updateSQL."
87 WHERE ".static::getDatabaseTableIndexName()." = ?";
88 $statement = WCF::getDB()->prepareStatement($sql);
89 $statement->execute($statementParameters);
90 }
91
92 /**
93 * @inheritDoc
94 */
95 public function delete() {
96 static::deleteAll([$this->getObjectID()]);
97 }
98
99 /**
100 * @inheritDoc
101 */
102 public static function deleteAll(array $objectIDs = []) {
103 $sql = "DELETE FROM ".static::getDatabaseTableName()."
104 WHERE ".static::getDatabaseTableIndexName()." = ?";
105 $statement = WCF::getDB()->prepareStatement($sql);
106
107 $affectedCount = 0;
108 WCF::getDB()->beginTransaction();
109 foreach ($objectIDs as $objectID) {
110 $statement->execute([$objectID]);
111 $affectedCount += $statement->getAffectedRows();
112 }
113 WCF::getDB()->commitTransaction();
114
115 return $affectedCount;
116 }
117 }