Created
January 19, 2017 11:44
-
-
Save pbowyer/97c779a5a0c74df91b8bd4d0eefc955b to your computer and use it in GitHub Desktop.
Doctrine DBAL extension for MySQL specific functionality
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Symfony configuration file | |
doctrine: | |
dbal: | |
... | |
wrapper_class: '\AppBundle\Doctrine\DBAL\Connection' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace TheCourseManager\AppBundle\Doctrine\DBAL; | |
/** | |
* A wrapper around a Doctrine\DBAL\Connection that adds MySQL specific features | |
* | |
*/ | |
class Connection extends \Doctrine\DBAL\Connection | |
{ | |
/** | |
* Inserts a table row with specified data. | |
* | |
* Table expression and columns are not escaped and are not safe for user-input. | |
* | |
* @param string $tableExpression The expression of the table to insert data into, quoted or unquoted. | |
* @param array $data An associative array containing column-value pairs. | |
* @param array $types Types of the inserted data. | |
* | |
* @return integer The number of affected rows. | |
*/ | |
public function insertIgnore($tableExpression, array $data, array $types = array()) | |
{ | |
$this->connect(); | |
if (empty($data)) { | |
return $this->executeUpdate('INSERT INTO ' . $tableExpression . ' ()' . ' VALUES ()'); | |
} | |
return $this->executeUpdate( | |
'INSERT IGNORE INTO ' . $tableExpression . ' (' . implode(', ', array_keys($data)) . ')' . | |
' VALUES (' . implode(', ', array_fill(0, count($data), '?')) . ')', | |
array_values($data), | |
is_string(key($types)) ? $this->extractTypeValues($data, $types) : $types | |
); | |
} | |
/** | |
* DUE TO PRIVATE DEFINITION, DUPLICATED FROM \Doctrine\DBAL\Connection | |
* | |
* @param array $data | |
* @param array $types | |
* | |
* @return array | |
*/ | |
private function extractTypeValues(array $data, array $types) | |
{ | |
$typeValues = array(); | |
foreach ($data as $k => $_) { | |
$typeValues[] = isset($types[$k]) | |
? $types[$k] | |
: \PDO::PARAM_STR; | |
} | |
return $typeValues; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment