6 * Basic implementation for object editors following the decorator pattern.
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
13 * @category Community Framework
15 abstract class DatabaseObjectEditor
extends DatabaseObjectDecorator
implements IEditableObject
{
17 * @see wcf\data\IEditableObject::create()
19 public static function create(array $parameters = array()) {
21 $statementParameters = array();
22 foreach ($parameters as $key => $value) {
30 $statementParameters[] = $value;
34 $sql = "INSERT INTO ".static::getDatabaseTableName()."
36 VALUES (".$values.")";
37 $statement = WCF
::getDB()->prepareStatement($sql);
38 $statement->execute($statementParameters);
41 if (static::getDatabaseTableIndexIsIdentity()) {
42 $id = WCF
::getDB()->getInsertID(static::getDatabaseTableName(), static::getDatabaseTableIndexName());
45 $id = $parameters[static::getDatabaseTableIndexName()];
47 return new static::$baseClass($id);
51 * @see wcf\data\IEditableObject::update()
53 public function update(array $parameters = array()) {
54 if (!count($parameters)) return;
56 // check whether any value changed or not
58 foreach ($parameters as $name => $value) {
59 if ($this->__get($name) != $value) {
65 // there is no new data - break to avoid senseless sql queries
69 $statementParameters = array();
70 foreach ($parameters as $key => $value) {
71 if (!empty($updateSQL)) $updateSQL .= ', ';
72 $updateSQL .= $key . ' = ?';
73 $statementParameters[] = $value;
75 $statementParameters[] = $this->__get(static::getDatabaseTableIndexName());
77 $sql = "UPDATE ".static::getDatabaseTableName()."
79 WHERE ".static::getDatabaseTableIndexName()." = ?";
80 $statement = WCF
::getDB()->prepareStatement($sql);
81 $statement->execute($statementParameters);
85 * @see wcf\data\IEditableObject::updateCounters()
87 public function updateCounters(array $counters = array()) {
88 if (!count($counters)) return;
91 $statementParameters = array();
92 foreach ($counters as $key => $value) {
93 if (!empty($updateSQL)) $updateSQL .= ', ';
94 $updateSQL .= $key . ' = ' . $key . ' + ?';
95 $statementParameters[] = $value;
97 $statementParameters[] = $this->__get(static::getDatabaseTableIndexName());
99 $sql = "UPDATE ".static::getDatabaseTableName()."
101 WHERE ".static::getDatabaseTableIndexName()." = ?";
102 $statement = WCF
::getDB()->prepareStatement($sql);
103 $statement->execute($statementParameters);
107 * @see wcf\data\IEditableObject::delete()
109 public function delete() {
110 static::deleteAll(array($this->__get(static::getDatabaseTableIndexName())));
114 * @see wcf\data\IEditableObject::deleteAll()
116 public static function deleteAll(array $objectIDs = array()) {
117 $sql = "DELETE FROM ".static::getDatabaseTableName()."
118 WHERE ".static::getDatabaseTableIndexName()." = ?";
119 $statement = WCF
::getDB()->prepareStatement($sql);
122 WCF
::getDB()->beginTransaction();
123 foreach ($objectIDs as $objectID) {
124 $statement->executeUnbuffered(array($objectID));
125 $affectedCount +
= $statement->getAffectedRows();
127 WCF
::getDB()->commitTransaction();
129 return $affectedCount;