/** * DatabaseModel->insert() Method * * Insert data into the database. * * @param array $in (required) Associative array of input to be inserted, keys being the name of columns * @param string $table (optional if defined in constructor) Table to use * @return Database reference to self */ public function insert($in, $table = null) { $typeof_table = gettype($table); if ($table != null) { if ($typeof_table != 'string') { throw new DatabaseException(__METHOD__ . '(): encountered table argument of invalid type.', DatabaseException::EXCEPTION_INPUT_INVALID_TYPE, $this); } else { if (!$this->tableExists($table)) { throw new DatabaseException(__METHOD__ . '(): table "' . $table . '" does not exist.', DatabaseException::EXCEPTION_INPUT_NOT_VALID, $this); } } } if (!$this->tableExists($table)) { $typeof_in = gettype($in); } if ($typeof_in == 'array') { //input is an array $sql = $this->dbms['sql']['insert']; //TODO: Generate data array so we can dynamically process input data. //generate the statement $query = self::genStmt($this->dbms['sql']['insert']['stmt'], isset($sql['tables']) ? $data[$sql['tables']['value']] : null, isset($sql['columns']) ? $data[$sql['columns']['value']] : null, isset($sql['sets']) ? $data[$sql['sets']['value']] : null, isset($sql['tablesets']) ? $data[$sql['tablesets']['value']] : null, isset($sql['columnsets']) ? $data[$sql['columnsets']['value']] : null, isset($sql['conditions']) ? $data[$sql['conditions']['value']] : null, $table); //prepare the statement $statement = $this->connector->prepare($query); //execute the statement $depth = DatabaseUtils::arrayMaxDepth($in); if ($depth == 1) { //array depth 1 - single row insert $executionArray = array($table); foreach ($in as $inKey => $inVal) { $executionArray[] = (string) $inKey; } foreach ($in as $inKey => $inVal) { $executionArray[] = (string) $inVal; } try { $statement->execute($executionArray); } catch (PDOException $e) { throw new DatabaseException(__METHOD__ . '(): caught exception thrown by PDO.', DatabaseException::EXCEPTION_GENERIC_DATABASE_ERROR, $this, $e); } } elseif ($depth == 2) { //array depth 2 - multi-row insert $status = array(); foreach ($in as $inVal) { $executionArray = array($table); foreach ($inVal as $inValKey => $inValVal) { $executionArray[] = (string) $inValKey; } foreach ($inVal as $inValKey => $inValVal) { $executionArray[] = (string) $inValVal; } try { $statement->execute($executionArray); } catch (PDOException $e) { throw new DatabaseException(__METHOD__ . '(): caught exception thrown by PDO.', DatabaseException::EXCEPTION_GENERIC_DATABASE_ERROR, $this, $e); } } } else { throw new DatabaseException(__METHOD__ . '(): encountered array of excessive depth. Max depth = 2.', DatabaseException::EXCEPTION_INPUT_ARRAY_TOO_DEEP, $this); } } else { throw new DatabaseException(__METHOD__ . '(): encountered input of invalid type. Must be an array.', DatabaseException::EXCEPTION_INPUT_INVALID_TYPE, $this); } return $this; //for method chaining (since this is a mutator) }