Commit 7aabee5e authored by jwage's avatar jwage

[2.0] Fixing empty insert sql statements (closes #2481)

parent 7c56bfa1
...@@ -1580,4 +1580,16 @@ abstract class AbstractPlatform ...@@ -1580,4 +1580,16 @@ abstract class AbstractPlatform
{ {
return $schemaElementName; return $schemaElementName;
} }
/**
* Get the insert sql for an empty insert statement
*
* @param string $tableName
* @param string $identifierColumnName
* @return string $sql
*/
public function getEmptyIdentityInsertSql($tableName, $identifierColumnName)
{
return 'INSERT INTO ' . $tableName . ' (' . $identifierColumnName . ') VALUES (null)';
}
} }
\ No newline at end of file
...@@ -506,4 +506,16 @@ class MsSqlPlatform extends AbstractPlatform ...@@ -506,4 +506,16 @@ class MsSqlPlatform extends AbstractPlatform
return $query; return $query;
} }
/**
* Get the insert sql for an empty insert statement
*
* @param string $tableName
* @param string $identifierColumnName
* @return string $sql
*/
public function getEmptyIdentityInsertSql($quotedTableName, $quotedIdentifierColumnName)
{
return 'INSERT INTO ' . $quotedTableName . ' DEFAULT VALUES';
}
} }
\ No newline at end of file
...@@ -770,4 +770,16 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -770,4 +770,16 @@ class PostgreSqlPlatform extends AbstractPlatform
{ {
return 'Y-m-d H:i:sO'; return 'Y-m-d H:i:sO';
} }
/**
* Get the insert sql for an empty insert statement
*
* @param string $tableName
* @param string $identifierColumnName
* @return string $sql
*/
public function getEmptyIdentityInsertSql($quotedTableName, $quotedIdentifierColumnName)
{
return 'INSERT INTO ' . $quotedTableName . ' (' . $quotedIdentifierColumnName . ') VALUES (DEFAULT)';
}
} }
\ No newline at end of file
...@@ -369,10 +369,17 @@ class ClassMetadataFactory ...@@ -369,10 +369,17 @@ class ClassMetadataFactory
$class->resultColumnNames[$this->_targetPlatform->getSqlResultCasing($columnName)] = $columnName; $class->resultColumnNames[$this->_targetPlatform->getSqlResultCasing($columnName)] = $columnName;
} }
$class->insertSql = 'INSERT INTO ' . if (empty($columns)) {
$class->getQuotedTableName($this->_targetPlatform) $class->insertSql = $this->_targetPlatform->getEmptyIdentityInsertSql(
. ' (' . implode(', ', $columns) . ') ' $class->getQuotedTableName($this->_targetPlatform),
. 'VALUES (' . implode(', ', $values) . ')'; $class->getQuotedColumnName($class->identifier[0], $this->_targetPlatform)
);
} else {
$class->insertSql = 'INSERT INTO ' .
$class->getQuotedTableName($this->_targetPlatform)
. ' (' . implode(', ', $columns) . ') '
. 'VALUES (' . implode(', ', $values) . ')';
}
} }
/** /**
......
...@@ -139,17 +139,19 @@ class StandardEntityPersister ...@@ -139,17 +139,19 @@ class StandardEntityPersister
$insertData = array(); $insertData = array();
$this->_prepareData($entity, $insertData, true); $this->_prepareData($entity, $insertData, true);
$paramIndex = 1; if (isset($insertData[$primaryTableName])) {
if ($sqlLogger) { $paramIndex = 1;
$params = array(); if ($sqlLogger) {
foreach ($insertData[$primaryTableName] as $value) { $params = array();
$params[$paramIndex] = $value; foreach ($insertData[$primaryTableName] as $value) {
$stmt->bindValue($paramIndex++, $value); $params[$paramIndex] = $value;
} $stmt->bindValue($paramIndex++, $value);
$sqlLogger->logSql($this->_class->insertSql, $params); }
} else { $sqlLogger->logSql($this->_class->insertSql, $params);
foreach ($insertData[$primaryTableName] as $value) { } else {
$stmt->bindValue($paramIndex++, $value); foreach ($insertData[$primaryTableName] as $value) {
$stmt->bindValue($paramIndex++, $value);
}
} }
} }
......
...@@ -43,6 +43,7 @@ class AllTests ...@@ -43,6 +43,7 @@ class AllTests
$suite->addTest(Locking\AllTests::suite()); $suite->addTest(Locking\AllTests::suite());
$suite->addTest(SchemaTool\AllTests::suite()); $suite->addTest(SchemaTool\AllTests::suite());
$suite->addTest(Ticket\AllTests::suite());
return $suite; return $suite;
} }
......
<?php
namespace Doctrine\Tests\ORM\Functional\Ticket;
if (!defined('PHPUnit_MAIN_METHOD')) {
define('PHPUnit_MAIN_METHOD', 'Orm_Functional_Ticket_AllTests::main');
}
require_once __DIR__ . '/../../../TestInit.php';
class AllTests
{
public static function main()
{
\PHPUnit_TextUI_TestRunner::run(self::suite());
}
public static function suite()
{
$suite = new \Doctrine\Tests\OrmFunctionalTestSuite('Doctrine Orm Ticket Tests');
$tests = glob(__DIR__ . '/Ticket*Test.php');
foreach ($tests as $test) {
$info = pathinfo($test);
$suite->addTestSuite('Doctrine\Tests\ORM\Functional\Ticket\\' . $info['filename']);
}
return $suite;
}
}
if (PHPUnit_MAIN_METHOD == 'Orm_Functional_Ticket_AllTests::main') {
AllTests::main();
}
<?php
namespace Doctrine\Tests\ORM\Functional\Ticket;
require_once __DIR__ . '/../../../TestInit.php';
class Ticket2481Test extends \Doctrine\Tests\OrmFunctionalTestCase
{
protected function setUp()
{
parent::setUp();
try {
$this->_schemaTool->createSchema(array(
$this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\Ticket\Ticket2481Product')
));
} catch (\Exception $e) {
// Swallow all exceptions. We do not test the schema tool here.
}
$this->_conn = $this->_em->getConnection();
}
public function testEmptyInsert()
{
$test = new Ticket2481Product();
$this->_em->persist($test);
$this->_em->flush();
$this->assertTrue($test->id > 0);
}
}
/**
* @Entity
* @Table(name="ticket_2481_products")
*/
class Ticket2481Product
{
/**
* @Id @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
public $id;
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment