Skip to content

Commit

Permalink
DDC-770 - Cleanup Query instance when its cloned
Browse files Browse the repository at this point in the history
  • Loading branch information
beberlei committed Aug 27, 2010
1 parent 241e4d2 commit 8a21ab4
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 22 deletions.
16 changes: 16 additions & 0 deletions lib/Doctrine/ORM/AbstractQuery.php
Expand Up @@ -138,10 +138,16 @@ public function getEntityManager()

/**
* Frees the resources used by the query object.
*
* Resets Parameters, Parameter Types and Query Hints.
*
* @return void
*/
public function free()
{
$this->_params = array();
$this->_paramTypes = array();
$this->_hints = array();
}

/**
Expand Down Expand Up @@ -569,4 +575,14 @@ protected function _getResultCacheId()
* @return Doctrine\DBAL\Driver\Statement The executed database statement that holds the results.
*/
abstract protected function _doExecute();

/**
* Cleanup Query resource when clone is called.
*
* @return void
*/
public function __clone()
{
$this->free();
}
}
22 changes: 0 additions & 22 deletions tests/Doctrine/Tests/ORM/Functional/QueryTest.php
Expand Up @@ -19,28 +19,6 @@ protected function setUp()
$this->useModelSet('cms');
parent::setUp();
}

/**
* @expectedException Doctrine\ORM\Query\QueryException
*/
public function testParameterIndexZeroThrowsException()
{
$query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username = ?1");
$query->execute(array(42)); // same as array(0 => 42), 0 is invalid parameter position
}

public function testGetParameters()
{
$query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username = ?1");
$this->assertEquals(array(), $query->getParameters());
}

public function testGetParameters_HasSomeAlready()
{
$query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username = ?1");
$query->setParameter(2, 84);
$this->assertEquals(array(2 => 84), $query->getParameters());
}

public function testSimpleQueries()
{
Expand Down
1 change: 1 addition & 0 deletions tests/Doctrine/Tests/ORM/Query/AllTests.php
Expand Up @@ -26,6 +26,7 @@ public static function suite()
$suite->addTestSuite('Doctrine\Tests\ORM\Query\UpdateSqlGenerationTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Query\ExprTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Query\ParserResultTest');
$suite->addTestSuite('Doctrine\Tests\ORM\Query\QueryTest');

return $suite;
}
Expand Down
47 changes: 47 additions & 0 deletions tests/Doctrine/Tests/ORM/Query/QueryTest.php
@@ -0,0 +1,47 @@
<?php

namespace Doctrine\Tests\ORM\Query;

require_once __DIR__ . '/../../TestInit.php';

class QueryTest extends \Doctrine\Tests\OrmTestCase
{
protected $_em = null;

protected function setUp()
{
$this->_em = $this->_getTestEntityManager();
}

/**
* @expectedException Doctrine\ORM\Query\QueryException
*/
public function testParameterIndexZeroThrowsException()
{
$query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username = ?1");
$query->execute(array(42)); // same as array(0 => 42), 0 is invalid parameter position
}

public function testGetParameters()
{
$query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username = ?1");
$this->assertEquals(array(), $query->getParameters());
}

public function testGetParameters_HasSomeAlready()
{
$query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username = ?1");
$query->setParameter(2, 84);
$this->assertEquals(array(2 => 84), $query->getParameters());
}

public function testFree()
{
$query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username = ?1");
$query->setParameter(2, 84, \PDO::PARAM_INT);

$query->free();

$this->assertEquals(array(), $query->getParameters());
}
}

0 comments on commit 8a21ab4

Please sign in to comment.