Skip to content

Commit

Permalink
[2.0][DDC-121] Fixed. Also fixed memcache tests and some small refact…
Browse files Browse the repository at this point in the history
…orings.
  • Loading branch information
romanb committed Nov 8, 2009
1 parent 7f725aa commit 8e3f6ee
Show file tree
Hide file tree
Showing 14 changed files with 84 additions and 30 deletions.
17 changes: 2 additions & 15 deletions lib/Doctrine/DBAL/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ public function fetchArray($statement, array $params = array())
}

/**
* 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 array $params prepared statement params
Expand All @@ -337,7 +337,7 @@ public function fetchArray($statement, array $params = array())
*/
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);
}

/**
Expand Down Expand Up @@ -536,19 +536,6 @@ public function fetchAll($sql, array $params = array())
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.
*
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Driver/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function prepare($prepareString);
function query();
function quote($input);
function exec($statement);
function lastInsertId();
function lastInsertId($name = null);
function beginTransaction();
function commit();
function rollBack();
Expand Down
3 changes: 2 additions & 1 deletion lib/Doctrine/DBAL/DriverManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ final class DriverManager
'pdo_sqlite' => 'Doctrine\DBAL\Driver\PDOSqlite\Driver',
'pdo_pgsql' => 'Doctrine\DBAL\Driver\PDOPgSql\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. */
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Id/SequenceGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function generate(EntityManager $em, $entity)
// Allocate new values
$conn = $em->getConnection();
$sql = $conn->getDatabasePlatform()->getSequenceNextValSql($this->_sequenceName);
$this->_nextValue = $conn->fetchOne($sql);
$this->_nextValue = $conn->fetchColumn($sql);
$this->_maxValue = $this->_nextValue + $this->_allocationSize;
}
return $this->_nextValue++;
Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/ORM/Internal/Hydration/SingleScalarHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ protected function _hydrateAll()
$cache = array();
$result = $this->_stmt->fetchAll(Connection::FETCH_ASSOC);
//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();
}
$result = $this->_gatherScalarRowData($result[0], $cache);
$result = $this->_gatherScalarRowData($result[key($result)], $cache);
return array_shift($result);
}

Expand Down
17 changes: 15 additions & 2 deletions lib/Doctrine/ORM/PersistentCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public function hydrateAdd($element)
} else {
// ManyToMany
$this->_typeClass->reflFields[$this->_backRefFieldName]
->getValue($element)->add($this->_owner);
->getValue($element)->unwrap()->add($this->_owner);
}
}
}
Expand Down Expand Up @@ -229,8 +229,18 @@ public function hydrateSet($key, $element)
private function _initialize()
{
if ( ! $this->_initialized) {
if ($this->_isDirty) {
// Has NEW objects added through add(). Remember them.
$newObjects = $this->_coll->toArray();
}
$this->_coll->clear();
$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;
}
}
Expand All @@ -242,6 +252,7 @@ private function _initialize()
public function takeSnapshot()
{
$this->_snapshot = $this->_coll->toArray();
$this->_isDirty = false;
}

/**
Expand All @@ -267,7 +278,8 @@ public function getDeleteDiff()
}

/**
* INTERNAL getInsertDiff
* INTERNAL:
* getInsertDiff
*
* @return array
*/
Expand Down Expand Up @@ -465,6 +477,7 @@ public function count()
*/
public function set($key, $value)
{
$this->_initialize();
$this->_coll->set($key, $value);
$this->_changed();
}
Expand Down
5 changes: 3 additions & 2 deletions lib/Doctrine/ORM/Persisters/StandardEntityPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ protected function _assignDefaultVersionValue($class, $entity, $id)
$sql = "SELECT " . $versionFieldColumnName . " FROM " . $class->getQuotedTableName($this->_platform) .
" WHERE " . implode(' = ? AND ', $identifier) . " = ?";
$value = $this->_conn->fetchColumn($sql, (array) $id);
$this->_class->setFieldValue($entity, $versionField, $value[0]);
$this->_class->setFieldValue($entity, $versionField, $value);
}

/**
Expand Down Expand Up @@ -550,7 +550,8 @@ public function loadManyToManyCollection($assoc, array $criteria, PersistentColl
$stmt = $this->_conn->prepare($this->_getSelectManyToManyEntityCollectionSql($assoc, $criteria));
$stmt->execute(array_values($criteria));
while ($result = $stmt->fetch(Connection::FETCH_ASSOC)) {
$coll->add($this->_createEntity($result));
//$coll->add($this->_createEntity($result));
$coll->hydrateAdd($this->_createEntity($result));
}
$stmt->closeCursor();
}
Expand Down
11 changes: 10 additions & 1 deletion tests/Doctrine/Tests/Common/Cache/MemcacheCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,25 @@

class MemcacheCacheTest extends \Doctrine\Tests\DoctrineTestCase
{
private $_memcache;

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');
}
}

public function testMemcacheCacheDriver()
{
$cache = new MemcacheCache();
$cache->setMemcache($this->_memcache);

// Test save
$cache->save('test_key', 'testing this out');
Expand Down
2 changes: 1 addition & 1 deletion tests/Doctrine/Tests/Mocks/ConnectionMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function lastInsertId($seqName = null)
/**
* @override
*/
public function fetchOne($statement, array $params = array(), $colnum = 0)
public function fetchColumn($statement, array $params = array(), $colnum = 0)
{
return $this->_fetchOneResult;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Doctrine/Tests/Mocks/DriverConnectionMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public function prepare($prepareString) {}
public function query() {}
public function quote($input) {}
public function exec($statement) {}
public function lastInsertId() {}
public function lastInsertId($name = null) {}
public function beginTransaction() {}
public function commit() {}
public function rollBack() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ public function testIssue()
$definitions = $res[0]->getDefinitions();

$this->assertEquals(1, count($res));

$this->assertTrue($definitions[0] instanceof Definition);
$this->assertEquals(2, $definitions->count());

}

public function testManyToMany()
Expand Down
42 changes: 42 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/BasicFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,48 @@ public function testAddToCollectionDoesNotInitialize()
$gblanco->addPhonenumber($newPhone);

$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->clear();
Expand Down
1 change: 1 addition & 0 deletions tests/Doctrine/Tests/ORM/Functional/IdentityMapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ public function testCollectionValuedAssociationIdentityMapBehaviorWithRefreshQue
$this->_em->flush();

$this->assertEquals(3, count($user->getPhonenumbers()));
$this->assertFalse($user->getPhonenumbers()->isDirty());

//external update to CmsAddress
$this->_em->getConnection()->executeUpdate('insert into cms_phonenumbers (phonenumber, user_id) VALUES (?,?)', array(999, $user->getId()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ public function testLazyLoadsCollectionOnTheOwningSide()
$this->assertLoadingOfOwningSide($products);
}


private function _createLoadingFixture()
{
$this->firstProduct->addCategory($this->firstCategory);
Expand Down Expand Up @@ -140,6 +139,7 @@ public function assertLoadingOfOwningSide($products)

$this->assertEquals(2, count($firstProductCategories));
$this->assertEquals(2, count($secondProductCategories));

$this->assertTrue($firstProductCategories[0] === $secondProductCategories[0]);
$this->assertTrue($firstProductCategories[1] === $secondProductCategories[1]);

Expand All @@ -148,7 +148,7 @@ public function assertLoadingOfOwningSide($products)

$this->assertEquals(2, count($firstCategoryProducts));
$this->assertEquals(2, count($secondCategoryProducts));

$this->assertTrue($firstCategoryProducts[0] instanceof ECommerceProduct);
$this->assertTrue($firstCategoryProducts[1] instanceof ECommerceProduct);
$this->assertTrue($secondCategoryProducts[0] instanceof ECommerceProduct);
Expand Down

0 comments on commit 8e3f6ee

Please sign in to comment.