src/SQL.php
final public function insert($table, $args=[], $pk=null) {

		$keys = $vals = [];
		$keys = array_keys($args);
		$vals = array_fill(0, count($args), '?');

		$columns = implode(',', $keys);
		$placeholders = implode(',', $vals);

		$stmt = "INSERT INTO $table ($columns) VALUES ($placeholders)";
		if ($this->dbtype == 'pgsql')
			$stmt .= " RETURNING " . ($pk ? $pk : '*');

		$pstmt = $this->prepare_statement($stmt, $args);

		if ($this->dbtype == 'pgsql') {
			$last = $pstmt->fetch(\PDO::FETCH_ASSOC);
			$ret = $pk ? $last[$pk] : $last;
		} else {
			$ret = $this->connection->lastInsertId();
		}

		self::$logger->info(sprintf(
			"SQL: insert ok: %s <- '%s'.",
			$stmt, json_encode($args)));
		return $ret;
	}
final public function insert($table, $args=[], $pk=null) {

		$keys = $vals = [];
		$keys = array_keys($args);
		$vals = array_fill(0, count($args), '?');

		$columns = implode(',', $keys);
		$placeholders = implode(',', $vals);

		$stmt = "INSERT INTO $table ($columns) VALUES ($placeholders)";
		if ($this->dbtype == 'pgsql')
			$stmt .= " RETURNING " . ($pk ? $pk : '*');

		$pstmt = $this->prepare_statement($stmt, $args);

		if ($this->dbtype == 'pgsql') {
			$last = $pstmt->fetch(\PDO::FETCH_ASSOC);
			$ret = $pk ? $last[$pk] : $last;
		} else {
			$ret = $this->connection->lastInsertId();
		}

		self::$logger->info(sprintf(
			"SQL: insert ok: %s <- '%s'.",
			$stmt, json_encode($args)));
		return $ret;
	}
final public function insert ( $ table , $ args = [ ] , $ pk = null ) { $ keys = $ vals = [ ] ; $ keys = array_keys ( $ args ) ; $ vals = array_fill ( 0 , count ( $ args ) , '?' ) ; $ columns = implode ( ',' , $ keys ) ; $ placeholders = implode ( ',' , $ vals ) ; $ stmt = "INSERT INTO $table ($columns) VALUES ($placeholders)" ; if ( $ this -> dbtype == 'pgsql' ) $ stmt .= " RETURNING " . ( $ pk ? $ pk : '*' ) ; $ pstmt = $ this -> prepare_statement ( $ stmt , $ args ) ; if ( $ this -> dbtype == 'pgsql' ) { $ last = $ pstmt -> fetch ( \ PDO :: FETCH_ASSOC ) ; $ ret = $ pk ? $ last [ $ pk ] : $ last ; } else { $ ret = $ this -> connection -> lastInsertId ( ) ; } self :: $ logger -> info ( sprintf ( "SQL: insert ok: %s <- '%s'." , $ stmt , json_encode ( $ args ) ) ) ; return $ ret ; }
Insert statement.

@param string $table Table name.
@param array $args Dict of what to INSERT.
@param string $pk Primary key from which last insert ID should
be retrieved. This won't take any effect on databases other
than Postgres. This can take any column name, not necessarily
column with PRIMARY KEY attributes. If left null, the whole
new row is returned as an array. Using invalid column will
throw exception.
@return int|array Last insert ID or IDs on success. Exception
thrown on error.
Insert statement .