From 306a31b673ca9cbb2a9dcefbe802ef429674cc7d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Mon, 15 Mar 2021 16:05:19 +0100 Subject: [PATCH] Add TFastCreate trait Co-authored-by: Alexander Ebert --- .../lib/data/DatabaseObjectEditor.class.php | 33 ++--------- .../files/lib/data/TFastCreate.class.php | 55 +++++++++++++++++++ 2 files changed, 60 insertions(+), 28 deletions(-) create mode 100644 wcfsetup/install/files/lib/data/TFastCreate.class.php diff --git a/wcfsetup/install/files/lib/data/DatabaseObjectEditor.class.php b/wcfsetup/install/files/lib/data/DatabaseObjectEditor.class.php index d0eb57927e..c4b209aef0 100644 --- a/wcfsetup/install/files/lib/data/DatabaseObjectEditor.class.php +++ b/wcfsetup/install/files/lib/data/DatabaseObjectEditor.class.php @@ -16,39 +16,16 @@ use wcf\system\WCF; */ abstract class DatabaseObjectEditor extends DatabaseObjectDecorator implements IEditableObject { + use TFastCreate { + TFastCreate::fastCreate as private dboEditorCreateBase; + } + /** * @inheritDoc */ public static function create(array $parameters = []) { - $keys = $values = ''; - $statementParameters = []; - foreach ($parameters as $key => $value) { - if (!empty($keys)) { - $keys .= ','; - $values .= ','; - } - - $keys .= $key; - $values .= '?'; - $statementParameters[] = $value; - } - - // save object - $sql = "INSERT INTO " . static::getDatabaseTableName() . " - (" . $keys . ") - VALUES (" . $values . ")"; - $statement = WCF::getDB()->prepareStatement($sql); - $statement->execute($statementParameters); - - // return new object - if (static::getDatabaseTableIndexIsIdentity()) { - $id = WCF::getDB()->getInsertID(static::getDatabaseTableName(), static::getDatabaseTableIndexName()); - } else { - $id = $parameters[static::getDatabaseTableIndexName()]; - } - - return new static::$baseClass($id); + return new static::$baseClass(static::dboEditorCreateBase($parameters)); } /** diff --git a/wcfsetup/install/files/lib/data/TFastCreate.class.php b/wcfsetup/install/files/lib/data/TFastCreate.class.php new file mode 100644 index 0000000000..b943e3cb58 --- /dev/null +++ b/wcfsetup/install/files/lib/data/TFastCreate.class.php @@ -0,0 +1,55 @@ + + * @package WoltLabSuite\Core\Data + * @since 5.4 + */ +trait TFastCreate +{ + /** + * Creates the object and returns the ID. + * + * @see IEditableObject::create() + * @return int|string + */ + public static function fastCreate(array $parameters) + { + $keys = $values = ''; + $statementParameters = []; + foreach ($parameters as $key => $value) { + if (!empty($keys)) { + $keys .= ','; + $values .= ','; + } + + $keys .= $key; + $values .= '?'; + $statementParameters[] = $value; + } + + // save object + $sql = "INSERT INTO " . static::getDatabaseTableName() . " + (" . $keys . ") + VALUES (" . $values . ")"; + $statement = WCF::getDB()->prepareStatement($sql); + $statement->execute($statementParameters); + + // return new object + if (static::getDatabaseTableIndexIsIdentity()) { + $id = WCF::getDB()->getInsertID(static::getDatabaseTableName(), static::getDatabaseTableIndexName()); + } else { + $id = $parameters[static::getDatabaseTableIndexName()]; + } + + return $id; + } +} -- 2.20.1