Merge pull request #12322 from totten/master-unused-modulepaths
[civicrm-core.git] / CRM / Utils / SQL / Insert.php
index 627473df145e1f8d9d1cc5108abad9958ce5b54d..fc8f1363583049ef5cded399acfd1190ad8ac363 100644 (file)
@@ -50,6 +50,28 @@ class CRM_Utils_SQL_Insert {
     return new self($table);
   }
 
+  /**
+   * Insert a record based on a DAO.
+   *
+   * @param \CRM_Core_DAO $dao
+   * @return \CRM_Utils_SQL_Insert
+   * @throws \CRM_Core_Exception
+   */
+  public static function dao(CRM_Core_DAO $dao) {
+    $table = CRM_Core_DAO::getLocaleTableName($dao->getTableName());
+    $row = array();
+    foreach ((array) $dao as $key => $value) {
+      if ($value === 'null') {
+        $value = NULL; // Blerg!!!
+      }
+      // Skip '_foobar' and '{\u00}*_options' and 'N'.
+      if (preg_match('/[a-zA-Z]/', $key{0}) && $key !== 'N') {
+        $row[$key] = $value;
+      }
+    }
+    return self::into($table)->row($row);
+  }
+
   /**
    * Create a new SELECT query.
    *
@@ -66,7 +88,7 @@ class CRM_Utils_SQL_Insert {
    *
    * @param array $columns
    *
-   * @return $this
+   * @return CRM_Utils_SQL_Insert
    * @throws \CRM_Core_Exception
    */
   public function columns($columns) {