Add TFastCreate trait
authorTim Düsterhus <duesterhus@woltlab.com>
Mon, 15 Mar 2021 15:05:19 +0000 (16:05 +0100)
committerTim Düsterhus <duesterhus@woltlab.com>
Mon, 15 Mar 2021 15:38:05 +0000 (16:38 +0100)
Co-authored-by: Alexander Ebert <ebert@woltlab.com>
wcfsetup/install/files/lib/data/DatabaseObjectEditor.class.php
wcfsetup/install/files/lib/data/TFastCreate.class.php [new file with mode: 0644]

index d0eb57927e23dc90423e9a0737cb72fe86a4e8fd..c4b209aef0e5c76cca3042380e75ac41db9384a8 100644 (file)
@@ -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 (file)
index 0000000..b943e3c
--- /dev/null
@@ -0,0 +1,55 @@
+<?php
+
+namespace wcf\data;
+
+use wcf\system\WCF;
+
+/**
+ * Adds a fastCreate() method that differs from create() by returning the ID only.
+ *
+ * @author  Tim Duesterhus
+ * @copyright   2001-2021 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @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;
+    }
+}