Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Remove unnecessary code from Connection::insert #314

Merged
merged 1 commit into from

4 participants

@deeky666
Collaborator

This is a tiny optimization in the Connection::insert method that remove an unnecessary foreach loop an some unneeded variable assignments.
If this is not desired, just close this PR ;)

@guilhermeblanco guilhermeblanco merged commit 5dd0adf into doctrine:master
@lastzero

This code breaks insert() calls with $data = array(), since array_fill() expects the second parameter to be positive (not 0). In MySQL, there can actually exist tables which have an automatic primary key and all other fields with default.

In case you do not want to accept empty $data for insert() anymore, you should 1) document that and 2) throw an exception which explains what's wrong (the warning triggered by array_fill() is not enough).

Why is there no automatic test for insert()? :)

Owner

@lastzero consider writing one and opening an issue or reporting the issue on http://www.doctrine-project.org/jira/browse/DBAL

Already cloned the repository... however, it won't be easy to write a test, if there is no existing approach for testing database methods like insert, update, delete, fetch,...? I usually do this with static file fixtures recorded from a database server during the first test run. Would this be ok?

The fix btw would just reverting this particular change, since the code obviously was not "unnecessary".

Thank you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 13, 2013
  1. @deeky666
This page is out of date. Refresh to see the latest.
Showing with 2 additions and 11 deletions.
  1. +2 −11 lib/Doctrine/DBAL/Connection.php
View
13 lib/Doctrine/DBAL/Connection.php
@@ -533,22 +533,13 @@ public function insert($tableName, array $data, array $types = array())
{
$this->connect();
- // column names are specified as array keys
- $cols = array();
- $placeholders = array();
-
- foreach ($data as $columnName => $value) {
- $cols[] = $columnName;
- $placeholders[] = '?';
- }
-
if ( ! is_int(key($types))) {
$types = $this->extractTypeValues($data, $types);
}
$query = 'INSERT INTO ' . $tableName
- . ' (' . implode(', ', $cols) . ')'
- . ' VALUES (' . implode(', ', $placeholders) . ')';
+ . ' (' . implode(', ', array_keys($data)) . ')'
+ . ' VALUES (' . implode(', ', array_fill(0, count($data), '?')) . ')';
return $this->executeUpdate($query, array_values($data), $types);
}
Something went wrong with that request. Please try again.