public function Insert($modelCollection, &$model) { $tableName = $modelCollection->ModelCache['MetaData']['TableName']; $columns = implode($modelCollection->ModelCache['MetaData']['ColumnNames'], ','); $valuePlaceHolders = implode(CreateArray('?', count($modelCollection->ModelCache['MetaData']['ColumnNames'])), ','); // Create the required SQL $sqlStatement = "INSERT INTO {$tableName}({$columns}) VALUES({$valuePlaceHolders});"; if (!($preparedStatement = $this->Database->prepare($sqlStatement))) { echo "Failed to prepare PDO statement"; var_dump($this->Database->errorInfo()); } $values = array(); foreach ($modelCollection->ModelCache['MetaData']['ColumnNames'] as $key) { $value = $model->{$key}; $values[] = $value; } /* $params = array(); foreach($values as $key => $value){ if($value == '0'){ $params[] = null; }else { $params[] = $values[$key]; } } */ if (!$preparedStatement->execute($values)) { echo "Failed to execute PDO statement"; var_dump($this->Database->errorInfo()); } $insertId = $this->Database->lastInsertId(); $primaryKey = $modelCollection->ModelCache['MetaData']['PrimaryKey']; $model->{$primaryKey} = $insertId; }
public function Insert($modelCollection, &$model) { $tableName = $modelCollection->ModelCache['MetaData']['TableName']; $columns = implode($modelCollection->ModelCache['MetaData']['ColumnNames'], ','); $valuePlaceHolders = implode(CreateArray('?', count($modelCollection->ModelCache['MetaData']['ColumnNames'])), ','); // Create the required SQL $sqlStatement = "INSERT INTO {$tableName}({$columns}) VALUES({$valuePlaceHolders});"; if (!($preparedStatement = $this->Database->prepare($sqlStatement))) { echo $this->Database->error; } $preparedStatementTypes = array(); $values = array(); foreach ($modelCollection->ModelCache['MetaData']['ColumnNames'] as $key) { $preparedStatementTypes[] = $modelCollection->ModelCache['Columns'][$key]['PreparedStatementType']; $values[] = $model->{$key}; } $preparedStatementTypes = implode($preparedStatementTypes); $params = array(); $params[] = $preparedStatementTypes; foreach ($values as $key => $value) { $params[] =& $values[$key]; } call_user_func_array(array($preparedStatement, 'bind_param'), $params); if (!$preparedStatement->execute()) { echo $this->Database->error; } $insertId = $this->Database->insert_id; $primaryKey = $modelCollection->ModelCache['MetaData']['PrimaryKey']; $model->{$primaryKey} = $insertId; $preparedStatement->close(); }