X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FUtils%2FSQL%2FInsert.php;h=816c89f605d0079eb949a02d1e2be230c31473f5;hb=394301964aa0322991744072dccbe6577abb0c47;hp=627473df145e1f8d9d1cc5108abad9958ce5b54d;hpb=a5b08a92f51c39c668edf05a5db3b199a300ddde;p=civicrm-core.git diff --git a/CRM/Utils/SQL/Insert.php b/CRM/Utils/SQL/Insert.php index 627473df14..816c89f605 100644 --- a/CRM/Utils/SQL/Insert.php +++ b/CRM/Utils/SQL/Insert.php @@ -36,6 +36,7 @@ class CRM_Utils_SQL_Insert { /** * Array list of column names + * @var array */ private $columns; @@ -50,6 +51,29 @@ 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 = []; + foreach ((array) $dao as $key => $value) { + if ($value === 'null') { + // Blerg!!! + $value = NULL; + } + // 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. * @@ -58,7 +82,7 @@ class CRM_Utils_SQL_Insert { */ public function __construct($table) { $this->table = $table; - $this->rows = array(); + $this->rows = []; } /** @@ -66,7 +90,7 @@ class CRM_Utils_SQL_Insert { * * @param array $columns * - * @return $this + * @return CRM_Utils_SQL_Insert * @throws \CRM_Core_Exception */ public function columns($columns) { @@ -106,11 +130,11 @@ class CRM_Utils_SQL_Insert { sort($columns); $this->columns = $columns; } - elseif (array_diff($this->columns, $columns) !== array()) { + elseif (array_diff($this->columns, $columns) !== []) { throw new CRM_Core_Exception("Inconsistent column names"); } - $escapedRow = array(); + $escapedRow = []; foreach ($this->columns as $column) { $escapedRow[$column] = $this->escapeString($row[$column]); }