/**
* Array<string> list of column names
+ * @var array
*/
private $columns;
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.
*
*/
public function __construct($table) {
$this->table = $table;
- $this->rows = array();
+ $this->rows = [];
}
/**
*
* @param array $columns
*
- * @return $this
+ * @return CRM_Utils_SQL_Insert
* @throws \CRM_Core_Exception
*/
public function columns($columns) {
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]);
}