6 * Basic implementation for object editors following the decorator pattern.
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
13 abstract class DatabaseObjectEditor
extends DatabaseObjectDecorator
implements IEditableObject
{
17 public static function create(array $parameters = []) {
19 $statementParameters = [];
20 foreach ($parameters as $key => $value) {
28 $statementParameters[] = $value;
32 $sql = "INSERT INTO ".static::getDatabaseTableName()."
34 VALUES (".$values.")";
35 $statement = WCF
::getDB()->prepareStatement($sql);
36 $statement->execute($statementParameters);
39 if (static::getDatabaseTableIndexIsIdentity()) {
40 $id = WCF
::getDB()->getInsertID(static::getDatabaseTableName(), static::getDatabaseTableIndexName());
43 $id = $parameters[static::getDatabaseTableIndexName()];
45 return new static::$baseClass($id);
51 public function update(array $parameters = []) {
52 if (empty($parameters)) return;
55 $statementParameters = [];
56 foreach ($parameters as $key => $value) {
57 if (!empty($updateSQL)) $updateSQL .= ', ';
58 $updateSQL .= $key . ' = ?';
59 $statementParameters[] = $value;
61 $statementParameters[] = $this->getObjectID();
63 $sql = "UPDATE ".static::getDatabaseTableName()."
65 WHERE ".static::getDatabaseTableIndexName()." = ?";
66 $statement = WCF
::getDB()->prepareStatement($sql);
67 $statement->execute($statementParameters);
73 public function updateCounters(array $counters = []) {
74 if (empty($counters)) return;
77 $statementParameters = [];
78 foreach ($counters as $key => $value) {
79 if (!empty($updateSQL)) $updateSQL .= ', ';
80 $updateSQL .= $key . ' = ' . $key . ' + ?';
81 $statementParameters[] = $value;
83 $statementParameters[] = $this->getObjectID();
85 $sql = "UPDATE ".static::getDatabaseTableName()."
87 WHERE ".static::getDatabaseTableIndexName()." = ?";
88 $statement = WCF
::getDB()->prepareStatement($sql);
89 $statement->execute($statementParameters);
95 public function delete() {
96 static::deleteAll([$this->getObjectID()]);
102 public static function deleteAll(array $objectIDs = []) {
103 $sql = "DELETE FROM ".static::getDatabaseTableName()."
104 WHERE ".static::getDatabaseTableIndexName()." = ?";
105 $statement = WCF
::getDB()->prepareStatement($sql);
108 WCF
::getDB()->beginTransaction();
109 foreach ($objectIDs as $objectID) {
110 $statement->execute([$objectID]);
111 $affectedCount +
= $statement->getAffectedRows();
113 WCF
::getDB()->commitTransaction();
115 return $affectedCount;