Commit 8e3f6eee authored by romanb's avatar romanb

[2.0][DDC-121] Fixed. Also fixed memcache tests and some small refactorings.

parent 7f725aa7
...@@ -328,7 +328,7 @@ class Connection ...@@ -328,7 +328,7 @@ class Connection
} }
/** /**
* Convenience method for PDO::query("...") followed by $stmt->fetchAll(PDO::FETCH_COLUMN, ...). * Convenience method for PDO::query("...") followed by $stmt->fetchColumn(...).
* *
* @param string $statement sql query to be executed * @param string $statement sql query to be executed
* @param array $params prepared statement params * @param array $params prepared statement params
...@@ -337,7 +337,7 @@ class Connection ...@@ -337,7 +337,7 @@ class Connection
*/ */
public function fetchColumn($statement, array $params = array(), $colnum = 0) public function fetchColumn($statement, array $params = array(), $colnum = 0)
{ {
return $this->execute($statement, $params)->fetchAll(Connection::FETCH_COLUMN, $colnum); return $this->execute($statement, $params)->fetchColumn($colnum);
} }
/** /**
...@@ -536,19 +536,6 @@ class Connection ...@@ -536,19 +536,6 @@ class Connection
return $this->execute($sql, $params)->fetchAll(Connection::FETCH_ASSOC); return $this->execute($sql, $params)->fetchAll(Connection::FETCH_ASSOC);
} }
/**
* Convenience method for PDO::query("...") followed by $stmt->fetchColumn().
*
* @param string $statement The SQL query.
* @param array $params The query parameters.
* @param int $colnum 0-indexed column number to retrieve
* @return mixed
*/
public function fetchOne($statement, array $params = array(), $colnum = 0)
{
return $this->execute($statement, $params)->fetchColumn($colnum);
}
/** /**
* Prepares an SQL statement. * Prepares an SQL statement.
* *
......
...@@ -35,7 +35,7 @@ interface Connection ...@@ -35,7 +35,7 @@ interface Connection
function query(); function query();
function quote($input); function quote($input);
function exec($statement); function exec($statement);
function lastInsertId(); function lastInsertId($name = null);
function beginTransaction(); function beginTransaction();
function commit(); function commit();
function rollBack(); function rollBack();
......
...@@ -43,7 +43,8 @@ final class DriverManager ...@@ -43,7 +43,8 @@ final class DriverManager
'pdo_sqlite' => 'Doctrine\DBAL\Driver\PDOSqlite\Driver', 'pdo_sqlite' => 'Doctrine\DBAL\Driver\PDOSqlite\Driver',
'pdo_pgsql' => 'Doctrine\DBAL\Driver\PDOPgSql\Driver', 'pdo_pgsql' => 'Doctrine\DBAL\Driver\PDOPgSql\Driver',
'pdo_oci' => 'Doctrine\DBAL\Driver\PDOOracle\Driver', 'pdo_oci' => 'Doctrine\DBAL\Driver\PDOOracle\Driver',
'pdo_mssql' => 'Doctrine\DBAL\Driver\PDOMsSql\Driver' 'pdo_mssql' => 'Doctrine\DBAL\Driver\PDOMsSql\Driver',
'oci8' => 'Doctrine\DBAL\Driver\OCI8\Driver'
); );
/** Private constructor. This class cannot be instantiated. */ /** Private constructor. This class cannot be instantiated. */
......
...@@ -62,7 +62,7 @@ class SequenceGenerator extends AbstractIdGenerator implements \Serializable ...@@ -62,7 +62,7 @@ class SequenceGenerator extends AbstractIdGenerator implements \Serializable
// Allocate new values // Allocate new values
$conn = $em->getConnection(); $conn = $em->getConnection();
$sql = $conn->getDatabasePlatform()->getSequenceNextValSql($this->_sequenceName); $sql = $conn->getDatabasePlatform()->getSequenceNextValSql($this->_sequenceName);
$this->_nextValue = $conn->fetchOne($sql); $this->_nextValue = $conn->fetchColumn($sql);
$this->_maxValue = $this->_nextValue + $this->_allocationSize; $this->_maxValue = $this->_nextValue + $this->_allocationSize;
} }
return $this->_nextValue++; return $this->_nextValue++;
......
...@@ -36,10 +36,10 @@ class SingleScalarHydrator extends AbstractHydrator ...@@ -36,10 +36,10 @@ class SingleScalarHydrator extends AbstractHydrator
$cache = array(); $cache = array();
$result = $this->_stmt->fetchAll(Connection::FETCH_ASSOC); $result = $this->_stmt->fetchAll(Connection::FETCH_ASSOC);
//TODO: Let this exception be raised by Query as QueryException //TODO: Let this exception be raised by Query as QueryException
if (count($result) > 1 || count($result[0]) > 1) { if (count($result) > 1 || count($result[key($result)]) > 1) {
throw HydrationException::nonUniqueResult(); throw HydrationException::nonUniqueResult();
} }
$result = $this->_gatherScalarRowData($result[0], $cache); $result = $this->_gatherScalarRowData($result[key($result)], $cache);
return array_shift($result); return array_shift($result);
} }
......
...@@ -190,7 +190,7 @@ final class PersistentCollection implements \Doctrine\Common\Collections\Collect ...@@ -190,7 +190,7 @@ final class PersistentCollection implements \Doctrine\Common\Collections\Collect
} else { } else {
// ManyToMany // ManyToMany
$this->_typeClass->reflFields[$this->_backRefFieldName] $this->_typeClass->reflFields[$this->_backRefFieldName]
->getValue($element)->add($this->_owner); ->getValue($element)->unwrap()->add($this->_owner);
} }
} }
} }
...@@ -229,8 +229,18 @@ final class PersistentCollection implements \Doctrine\Common\Collections\Collect ...@@ -229,8 +229,18 @@ final class PersistentCollection implements \Doctrine\Common\Collections\Collect
private function _initialize() private function _initialize()
{ {
if ( ! $this->_initialized) { if ( ! $this->_initialized) {
if ($this->_isDirty) {
// Has NEW objects added through add(). Remember them.
$newObjects = $this->_coll->toArray();
}
$this->_coll->clear(); $this->_coll->clear();
$this->_association->load($this->_owner, $this, $this->_em); $this->_association->load($this->_owner, $this, $this->_em);
// Reattach NEW objects added through add(), if any.
if (isset($newObjects)) {
foreach ($newObjects as $obj) {
$this->_coll->add($obj);
}
}
$this->_initialized = true; $this->_initialized = true;
} }
} }
...@@ -242,6 +252,7 @@ final class PersistentCollection implements \Doctrine\Common\Collections\Collect ...@@ -242,6 +252,7 @@ final class PersistentCollection implements \Doctrine\Common\Collections\Collect
public function takeSnapshot() public function takeSnapshot()
{ {
$this->_snapshot = $this->_coll->toArray(); $this->_snapshot = $this->_coll->toArray();
$this->_isDirty = false;
} }
/** /**
...@@ -267,7 +278,8 @@ final class PersistentCollection implements \Doctrine\Common\Collections\Collect ...@@ -267,7 +278,8 @@ final class PersistentCollection implements \Doctrine\Common\Collections\Collect
} }
/** /**
* INTERNAL getInsertDiff * INTERNAL:
* getInsertDiff
* *
* @return array * @return array
*/ */
...@@ -465,6 +477,7 @@ final class PersistentCollection implements \Doctrine\Common\Collections\Collect ...@@ -465,6 +477,7 @@ final class PersistentCollection implements \Doctrine\Common\Collections\Collect
*/ */
public function set($key, $value) public function set($key, $value)
{ {
$this->_initialize();
$this->_coll->set($key, $value); $this->_coll->set($key, $value);
$this->_changed(); $this->_changed();
} }
......
...@@ -193,7 +193,7 @@ class StandardEntityPersister ...@@ -193,7 +193,7 @@ class StandardEntityPersister
$sql = "SELECT " . $versionFieldColumnName . " FROM " . $class->getQuotedTableName($this->_platform) . $sql = "SELECT " . $versionFieldColumnName . " FROM " . $class->getQuotedTableName($this->_platform) .
" WHERE " . implode(' = ? AND ', $identifier) . " = ?"; " WHERE " . implode(' = ? AND ', $identifier) . " = ?";
$value = $this->_conn->fetchColumn($sql, (array) $id); $value = $this->_conn->fetchColumn($sql, (array) $id);
$this->_class->setFieldValue($entity, $versionField, $value[0]); $this->_class->setFieldValue($entity, $versionField, $value);
} }
/** /**
...@@ -550,7 +550,8 @@ class StandardEntityPersister ...@@ -550,7 +550,8 @@ class StandardEntityPersister
$stmt = $this->_conn->prepare($this->_getSelectManyToManyEntityCollectionSql($assoc, $criteria)); $stmt = $this->_conn->prepare($this->_getSelectManyToManyEntityCollectionSql($assoc, $criteria));
$stmt->execute(array_values($criteria)); $stmt->execute(array_values($criteria));
while ($result = $stmt->fetch(Connection::FETCH_ASSOC)) { while ($result = $stmt->fetch(Connection::FETCH_ASSOC)) {
$coll->add($this->_createEntity($result)); //$coll->add($this->_createEntity($result));
$coll->hydrateAdd($this->_createEntity($result));
} }
$stmt->closeCursor(); $stmt->closeCursor();
} }
......
...@@ -8,9 +8,17 @@ require_once __DIR__ . '/../../TestInit.php'; ...@@ -8,9 +8,17 @@ require_once __DIR__ . '/../../TestInit.php';
class MemcacheCacheTest extends \Doctrine\Tests\DoctrineTestCase class MemcacheCacheTest extends \Doctrine\Tests\DoctrineTestCase
{ {
private $_memcache;
public function setUp() public function setUp()
{ {
if ( ! extension_loaded('memcache')) { if (extension_loaded('memcache')) {
$memcache = new \Memcache;
$ok = @$memcache->connect('localhost', 11211);
if (!$ok) {
$this->markTestSkipped('The ' . __CLASS__ .' requires the use of memcache');
}
} else {
$this->markTestSkipped('The ' . __CLASS__ .' requires the use of memcache'); $this->markTestSkipped('The ' . __CLASS__ .' requires the use of memcache');
} }
} }
...@@ -18,6 +26,7 @@ class MemcacheCacheTest extends \Doctrine\Tests\DoctrineTestCase ...@@ -18,6 +26,7 @@ class MemcacheCacheTest extends \Doctrine\Tests\DoctrineTestCase
public function testMemcacheCacheDriver() public function testMemcacheCacheDriver()
{ {
$cache = new MemcacheCache(); $cache = new MemcacheCache();
$cache->setMemcache($this->_memcache);
// Test save // Test save
$cache->save('test_key', 'testing this out'); $cache->save('test_key', 'testing this out');
......
...@@ -43,7 +43,7 @@ class ConnectionMock extends \Doctrine\DBAL\Connection ...@@ -43,7 +43,7 @@ class ConnectionMock extends \Doctrine\DBAL\Connection
/** /**
* @override * @override
*/ */
public function fetchOne($statement, array $params = array(), $colnum = 0) public function fetchColumn($statement, array $params = array(), $colnum = 0)
{ {
return $this->_fetchOneResult; return $this->_fetchOneResult;
} }
......
...@@ -8,7 +8,7 @@ class DriverConnectionMock implements \Doctrine\DBAL\Driver\Connection ...@@ -8,7 +8,7 @@ class DriverConnectionMock implements \Doctrine\DBAL\Driver\Connection
public function query() {} public function query() {}
public function quote($input) {} public function quote($input) {}
public function exec($statement) {} public function exec($statement) {}
public function lastInsertId() {} public function lastInsertId($name = null) {}
public function beginTransaction() {} public function beginTransaction() {}
public function commit() {} public function commit() {}
public function rollBack() {} public function rollBack() {}
......
...@@ -94,9 +94,9 @@ class AdvancedAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase ...@@ -94,9 +94,9 @@ class AdvancedAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase
$definitions = $res[0]->getDefinitions(); $definitions = $res[0]->getDefinitions();
$this->assertEquals(1, count($res)); $this->assertEquals(1, count($res));
$this->assertTrue($definitions[0] instanceof Definition); $this->assertTrue($definitions[0] instanceof Definition);
$this->assertEquals(2, $definitions->count()); $this->assertEquals(2, $definitions->count());
} }
public function testManyToMany() public function testManyToMany()
......
...@@ -380,6 +380,48 @@ class BasicFunctionalTest extends \Doctrine\Tests\OrmFunctionalTestCase ...@@ -380,6 +380,48 @@ class BasicFunctionalTest extends \Doctrine\Tests\OrmFunctionalTestCase
$gblanco->addPhonenumber($newPhone); $gblanco->addPhonenumber($newPhone);
$this->assertFalse($gblanco->getPhonenumbers()->isInitialized()); $this->assertFalse($gblanco->getPhonenumbers()->isInitialized());
$this->_em->persist($gblanco);
$this->_em->flush();
$this->_em->clear();
$query = $this->_em->createQuery("select u, p from Doctrine\Tests\Models\CMS\CmsUser u join u.phonenumbers p where u.username='gblanco'");
$gblanco2 = $query->getSingleResult();
$this->assertEquals(4, $gblanco2->getPhonenumbers()->count());
}
public function testInitializeCollectionWithNewObjectsRetainsNewObjects()
{
$user = new CmsUser;
$user->name = 'Guilherme';
$user->username = 'gblanco';
$user->status = 'developer';
for ($i=0; $i<3; ++$i) {
$phone = new CmsPhonenumber;
$phone->phonenumber = 100 + $i;
$user->addPhonenumber($phone);
}
$this->_em->persist($user);
$this->_em->flush();
$this->_em->clear();
$this->assertEquals(3, $user->getPhonenumbers()->count());
$query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username='gblanco'");
$gblanco = $query->getSingleResult();
$this->assertFalse($gblanco->getPhonenumbers()->isInitialized());
$newPhone = new CmsPhonenumber;
$newPhone->phonenumber = 555;
$gblanco->addPhonenumber($newPhone);
$this->assertFalse($gblanco->getPhonenumbers()->isInitialized());
$this->assertEquals(4, $gblanco->getPhonenumbers()->count());
$this->assertTrue($gblanco->getPhonenumbers()->isInitialized());
$this->_em->flush(); $this->_em->flush();
$this->_em->clear(); $this->_em->clear();
......
...@@ -178,6 +178,7 @@ class IdentityMapTest extends \Doctrine\Tests\OrmFunctionalTestCase ...@@ -178,6 +178,7 @@ class IdentityMapTest extends \Doctrine\Tests\OrmFunctionalTestCase
$this->_em->flush(); $this->_em->flush();
$this->assertEquals(3, count($user->getPhonenumbers())); $this->assertEquals(3, count($user->getPhonenumbers()));
$this->assertFalse($user->getPhonenumbers()->isDirty());
//external update to CmsAddress //external update to CmsAddress
$this->_em->getConnection()->executeUpdate('insert into cms_phonenumbers (phonenumber, user_id) VALUES (?,?)', array(999, $user->getId())); $this->_em->getConnection()->executeUpdate('insert into cms_phonenumbers (phonenumber, user_id) VALUES (?,?)', array(999, $user->getId()));
......
...@@ -110,7 +110,6 @@ class ManyToManyBidirectionalAssociationTest extends AbstractManyToManyAssociati ...@@ -110,7 +110,6 @@ class ManyToManyBidirectionalAssociationTest extends AbstractManyToManyAssociati
$this->assertLoadingOfOwningSide($products); $this->assertLoadingOfOwningSide($products);
} }
private function _createLoadingFixture() private function _createLoadingFixture()
{ {
$this->firstProduct->addCategory($this->firstCategory); $this->firstProduct->addCategory($this->firstCategory);
...@@ -140,6 +139,7 @@ class ManyToManyBidirectionalAssociationTest extends AbstractManyToManyAssociati ...@@ -140,6 +139,7 @@ class ManyToManyBidirectionalAssociationTest extends AbstractManyToManyAssociati
$this->assertEquals(2, count($firstProductCategories)); $this->assertEquals(2, count($firstProductCategories));
$this->assertEquals(2, count($secondProductCategories)); $this->assertEquals(2, count($secondProductCategories));
$this->assertTrue($firstProductCategories[0] === $secondProductCategories[0]); $this->assertTrue($firstProductCategories[0] === $secondProductCategories[0]);
$this->assertTrue($firstProductCategories[1] === $secondProductCategories[1]); $this->assertTrue($firstProductCategories[1] === $secondProductCategories[1]);
...@@ -148,7 +148,7 @@ class ManyToManyBidirectionalAssociationTest extends AbstractManyToManyAssociati ...@@ -148,7 +148,7 @@ class ManyToManyBidirectionalAssociationTest extends AbstractManyToManyAssociati
$this->assertEquals(2, count($firstCategoryProducts)); $this->assertEquals(2, count($firstCategoryProducts));
$this->assertEquals(2, count($secondCategoryProducts)); $this->assertEquals(2, count($secondCategoryProducts));
$this->assertTrue($firstCategoryProducts[0] instanceof ECommerceProduct); $this->assertTrue($firstCategoryProducts[0] instanceof ECommerceProduct);
$this->assertTrue($firstCategoryProducts[1] instanceof ECommerceProduct); $this->assertTrue($firstCategoryProducts[1] instanceof ECommerceProduct);
$this->assertTrue($secondCategoryProducts[0] instanceof ECommerceProduct); $this->assertTrue($secondCategoryProducts[0] instanceof ECommerceProduct);
......
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