mirror of
https://github.com/doctrine/orm.git
synced 2026-04-01 03:42:27 +02:00
Compare commits
33 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e6c434196c | ||
|
|
d52dbe62ac | ||
|
|
b0ede40f47 | ||
|
|
3645a9c44d | ||
|
|
39ce6f96a0 | ||
|
|
e43f5304ef | ||
|
|
67724eb7ae | ||
|
|
8d4bc0638d | ||
|
|
81186105b6 | ||
|
|
beef8acdf5 | ||
|
|
26fc8d60e6 | ||
|
|
12e8ab216a | ||
|
|
dac1a16964 | ||
|
|
d9821d3fda | ||
|
|
576a4d7e31 | ||
|
|
eaee924180 | ||
|
|
cf941ce54f | ||
|
|
cfb7461f51 | ||
|
|
569c08ce55 | ||
|
|
295523cdca | ||
|
|
25efabdb74 | ||
|
|
1d96178097 | ||
|
|
20cb50451d | ||
|
|
0ff512ba8f | ||
|
|
5e9014fd99 | ||
|
|
a90cd9dfe8 | ||
|
|
4d699789a2 | ||
|
|
1486c8f8e2 | ||
|
|
3dadfa49d5 | ||
|
|
9b36947a48 | ||
|
|
2122297fdb | ||
|
|
af99cba28c | ||
|
|
9bcee455ca |
@@ -18,7 +18,7 @@
|
||||
"doctrine/collections": "~1.2",
|
||||
"doctrine/dbal": ">=2.5-dev,<2.6-dev",
|
||||
"doctrine/instantiator": "~1.0.1",
|
||||
"doctrine/common": ">=2.5-dev,<2.7-dev",
|
||||
"doctrine/common": ">=2.5-dev,<2.8-dev",
|
||||
"doctrine/cache": "~1.4",
|
||||
"symfony/console": "~2.5|~3.0"
|
||||
},
|
||||
|
||||
@@ -1038,7 +1038,7 @@ abstract class AbstractQuery
|
||||
|
||||
$metadata = $this->_em->getClassMetadata($entityName);
|
||||
|
||||
return new Cache\TimestampCacheKey($metadata->getTableName());
|
||||
return new Cache\TimestampCacheKey($metadata->rootEntityName);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -32,6 +32,7 @@ use Doctrine\Common\Util\ClassUtils;
|
||||
|
||||
/**
|
||||
* @author Fabio B. Silva <fabio.bat.silva@gmail.com>
|
||||
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
||||
* @since 2.5
|
||||
*/
|
||||
abstract class AbstractCollectionPersister implements CachedCollectionPersister
|
||||
@@ -164,10 +165,18 @@ abstract class AbstractCollectionPersister implements CachedCollectionPersister
|
||||
public function storeCollectionCache(CollectionCacheKey $key, $elements)
|
||||
{
|
||||
/* @var $targetPersister CachedEntityPersister */
|
||||
$associationMapping = $this->sourceEntity->associationMappings[$key->association];
|
||||
$targetPersister = $this->uow->getEntityPersister($this->targetEntity->rootEntityName);
|
||||
$targetRegion = $targetPersister->getCacheRegion();
|
||||
$targetHydrator = $targetPersister->getEntityHydrator();
|
||||
$entry = $this->hydrator->buildCacheEntry($this->targetEntity, $key, $elements);
|
||||
|
||||
// Only preserve ordering if association configured it
|
||||
if ( ! (isset($associationMapping['indexBy']) && $associationMapping['indexBy'])) {
|
||||
// Elements may be an array or a Collection
|
||||
$elements = array_values(is_array($elements) ? $elements : $elements->getValues());
|
||||
}
|
||||
|
||||
$entry = $this->hydrator->buildCacheEntry($this->targetEntity, $key, $elements);
|
||||
|
||||
foreach ($entry->identifiers as $index => $entityKey) {
|
||||
if ($targetRegion->contains($entityKey)) {
|
||||
|
||||
@@ -131,7 +131,7 @@ abstract class AbstractEntityPersister implements CachedEntityPersister
|
||||
$this->cacheLogger = $cacheConfig->getCacheLogger();
|
||||
$this->timestampRegion = $cacheFactory->getTimestampRegion();
|
||||
$this->hydrator = $cacheFactory->buildEntityHydrator($em, $class);
|
||||
$this->timestampKey = new TimestampCacheKey($this->class->getTableName());
|
||||
$this->timestampKey = new TimestampCacheKey($this->class->rootEntityName);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -27,6 +27,7 @@ use Doctrine\ORM\Cache\EntityCacheKey;
|
||||
* Specific non-strict read/write cached entity persister
|
||||
*
|
||||
* @author Fabio B. Silva <fabio.bat.silva@gmail.com>
|
||||
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
||||
* @since 2.5
|
||||
*/
|
||||
class NonStrictReadWriteCachedEntityPersister extends AbstractEntityPersister
|
||||
@@ -78,13 +79,16 @@ class NonStrictReadWriteCachedEntityPersister extends AbstractEntityPersister
|
||||
*/
|
||||
public function delete($entity)
|
||||
{
|
||||
$key = new EntityCacheKey($this->class->rootEntityName, $this->uow->getEntityIdentifier($entity));
|
||||
$key = new EntityCacheKey($this->class->rootEntityName, $this->uow->getEntityIdentifier($entity));
|
||||
$deleted = $this->persister->delete($entity);
|
||||
|
||||
if ($this->persister->delete($entity)) {
|
||||
if ($deleted) {
|
||||
$this->region->evict($key);
|
||||
}
|
||||
|
||||
$this->queuedCache['delete'][] = $key;
|
||||
|
||||
return $deleted;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -30,6 +30,7 @@ use Doctrine\ORM\Cache\EntityCacheKey;
|
||||
* Specific read-write entity persister
|
||||
*
|
||||
* @author Fabio B. Silva <fabio.bat.silva@gmail.com>
|
||||
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
||||
* @since 2.5
|
||||
*/
|
||||
class ReadWriteCachedEntityPersister extends AbstractEntityPersister
|
||||
@@ -100,21 +101,24 @@ class ReadWriteCachedEntityPersister extends AbstractEntityPersister
|
||||
*/
|
||||
public function delete($entity)
|
||||
{
|
||||
$key = new EntityCacheKey($this->class->rootEntityName, $this->uow->getEntityIdentifier($entity));
|
||||
$lock = $this->region->lock($key);
|
||||
$key = new EntityCacheKey($this->class->rootEntityName, $this->uow->getEntityIdentifier($entity));
|
||||
$lock = $this->region->lock($key);
|
||||
$deleted = $this->persister->delete($entity);
|
||||
|
||||
if ($this->persister->delete($entity)) {
|
||||
if ($deleted) {
|
||||
$this->region->evict($key);
|
||||
}
|
||||
|
||||
if ($lock === null) {
|
||||
return;
|
||||
return $deleted;
|
||||
}
|
||||
|
||||
$this->queuedCache['delete'][] = array(
|
||||
'lock' => $lock,
|
||||
'key' => $key
|
||||
);
|
||||
|
||||
return $deleted;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -536,6 +536,8 @@ final class PersistentCollection extends AbstractLazyCollection implements Selec
|
||||
public function clear()
|
||||
{
|
||||
if ($this->initialized && $this->isEmpty()) {
|
||||
$this->collection->clear();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -90,10 +90,6 @@ abstract class AbstractCollectionPersister implements CollectionPersister
|
||||
|
||||
// If Entity is scheduled for inclusion, it is not in this collection.
|
||||
// We can assure that because it would have return true before on array check
|
||||
if ($entityState === UnitOfWork::STATE_MANAGED && $this->uow->isScheduledForInsert($entity)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
return ! ($entityState === UnitOfWork::STATE_MANAGED && $this->uow->isScheduledForInsert($entity));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -733,7 +733,6 @@ class UnitOfWork implements PropertyChangedListener
|
||||
|
||||
// Look for changes in associations of the entity
|
||||
foreach ($class->associationMappings as $field => $assoc) {
|
||||
|
||||
if (($val = $class->reflFields[$field]->getValue($entity)) === null) {
|
||||
continue;
|
||||
}
|
||||
@@ -799,7 +798,7 @@ class UnitOfWork implements PropertyChangedListener
|
||||
// Only MANAGED entities that are NOT SCHEDULED FOR INSERTION OR DELETION are processed here.
|
||||
$oid = spl_object_hash($entity);
|
||||
|
||||
if ( ! isset($this->entityInsertions[$oid]) && ! isset($this->entityDeletions[$oid]) && isset($this->entityStates[$oid])) {
|
||||
if ( ! isset($this->entityInsertions[$oid]) && ! isset($this->entityDeletions[$oid]) && isset($this->entityStates[$oid])) {
|
||||
$this->computeChangeSet($class, $entity);
|
||||
}
|
||||
}
|
||||
@@ -826,10 +825,7 @@ class UnitOfWork implements PropertyChangedListener
|
||||
if ($value instanceof PersistentCollection && $value->isDirty()) {
|
||||
$coid = spl_object_hash($value);
|
||||
|
||||
if ($assoc['isOwningSide']) {
|
||||
$this->collectionUpdates[$coid] = $value;
|
||||
}
|
||||
|
||||
$this->collectionUpdates[$coid] = $value;
|
||||
$this->visitedCollections[$coid] = $value;
|
||||
}
|
||||
|
||||
@@ -1803,7 +1799,7 @@ class UnitOfWork implements PropertyChangedListener
|
||||
* @throws OptimisticLockException If the entity uses optimistic locking through a version
|
||||
* attribute and the version check against the managed copy fails.
|
||||
* @throws ORMInvalidArgumentException If the entity instance is NEW.
|
||||
* @throws EntityNotFoundException
|
||||
* @throws EntityNotFoundException if an assigned identifier is used in the entity, but none is provided
|
||||
*/
|
||||
private function doMerge($entity, array &$visited, $prevManagedCopy = null, $assoc = null)
|
||||
{
|
||||
@@ -1835,6 +1831,7 @@ class UnitOfWork implements PropertyChangedListener
|
||||
if ( ! $id) {
|
||||
$managedCopy = $this->newInstance($class);
|
||||
|
||||
$this->mergeEntityStateIntoManagedCopy($entity, $managedCopy);
|
||||
$this->persistNew($class, $managedCopy);
|
||||
} else {
|
||||
$flatId = ($class->containsForeignIdentifier)
|
||||
@@ -1866,31 +1863,16 @@ class UnitOfWork implements PropertyChangedListener
|
||||
$managedCopy = $this->newInstance($class);
|
||||
$class->setIdentifierValues($managedCopy, $id);
|
||||
|
||||
$this->mergeEntityStateIntoManagedCopy($entity, $managedCopy);
|
||||
$this->persistNew($class, $managedCopy);
|
||||
}
|
||||
}
|
||||
|
||||
if ($class->isVersioned && $this->isLoaded($managedCopy) && $this->isLoaded($entity)) {
|
||||
$reflField = $class->reflFields[$class->versionField];
|
||||
$managedCopyVersion = $reflField->getValue($managedCopy);
|
||||
$entityVersion = $reflField->getValue($entity);
|
||||
|
||||
// Throw exception if versions don't match.
|
||||
if ($managedCopyVersion != $entityVersion) {
|
||||
throw OptimisticLockException::lockFailedVersionMismatch($entity, $entityVersion, $managedCopyVersion);
|
||||
} else {
|
||||
$this->ensureVersionMatch($class, $entity, $managedCopy);
|
||||
$this->mergeEntityStateIntoManagedCopy($entity, $managedCopy);
|
||||
}
|
||||
}
|
||||
|
||||
$visited[$oid] = $managedCopy; // mark visited
|
||||
|
||||
if ($this->isLoaded($entity)) {
|
||||
if ($managedCopy instanceof Proxy && ! $managedCopy->__isInitialized()) {
|
||||
$managedCopy->__load();
|
||||
}
|
||||
|
||||
$this->mergeEntityStateIntoManagedCopy($entity, $managedCopy);
|
||||
}
|
||||
|
||||
if ($class->isChangeTrackingDeferredExplicit()) {
|
||||
$this->scheduleForDirtyCheck($entity);
|
||||
}
|
||||
@@ -1908,6 +1890,33 @@ class UnitOfWork implements PropertyChangedListener
|
||||
return $managedCopy;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ClassMetadata $class
|
||||
* @param object $entity
|
||||
* @param object $managedCopy
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws OptimisticLockException
|
||||
*/
|
||||
private function ensureVersionMatch(ClassMetadata $class, $entity, $managedCopy)
|
||||
{
|
||||
if (! ($class->isVersioned && $this->isLoaded($managedCopy) && $this->isLoaded($entity))) {
|
||||
return;
|
||||
}
|
||||
|
||||
$reflField = $class->reflFields[$class->versionField];
|
||||
$managedCopyVersion = $reflField->getValue($managedCopy);
|
||||
$entityVersion = $reflField->getValue($entity);
|
||||
|
||||
// Throw exception if versions don't match.
|
||||
if ($managedCopyVersion == $entityVersion) {
|
||||
return;
|
||||
}
|
||||
|
||||
throw OptimisticLockException::lockFailedVersionMismatch($entity, $entityVersion, $managedCopyVersion);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if an entity is loaded - must either be a loaded proxy or not a proxy
|
||||
*
|
||||
@@ -3360,6 +3369,14 @@ class UnitOfWork implements PropertyChangedListener
|
||||
*/
|
||||
private function mergeEntityStateIntoManagedCopy($entity, $managedCopy)
|
||||
{
|
||||
if (! $this->isLoaded($entity)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (! $this->isLoaded($managedCopy)) {
|
||||
$managedCopy->__load();
|
||||
}
|
||||
|
||||
$class = $this->em->getClassMetadata(get_class($entity));
|
||||
|
||||
foreach ($this->reflectionPropertiesGetter->getProperties($class->name) as $prop) {
|
||||
|
||||
@@ -33,7 +33,7 @@ class State
|
||||
protected $country;
|
||||
|
||||
/**
|
||||
* @Cache
|
||||
* @Cache("NONSTRICT_READ_WRITE")
|
||||
* @OneToMany(targetEntity="City", mappedBy="state")
|
||||
*/
|
||||
protected $cities;
|
||||
|
||||
@@ -26,7 +26,7 @@ class Traveler
|
||||
protected $name;
|
||||
|
||||
/**
|
||||
* @Cache()
|
||||
* @Cache("NONSTRICT_READ_WRITE")
|
||||
* @OneToMany(targetEntity="Travel", mappedBy="traveler", cascade={"persist", "remove"}, orphanRemoval=true)
|
||||
*
|
||||
* @var \Doctrine\Common\Collections\Collection
|
||||
|
||||
@@ -6,17 +6,17 @@ class CompanyContractListener
|
||||
{
|
||||
public $postPersistCalls;
|
||||
public $prePersistCalls;
|
||||
|
||||
|
||||
public $postUpdateCalls;
|
||||
public $preUpdateCalls;
|
||||
|
||||
|
||||
public $postRemoveCalls;
|
||||
public $preRemoveCalls;
|
||||
|
||||
public $preFlushCalls;
|
||||
|
||||
|
||||
public $postLoadCalls;
|
||||
|
||||
|
||||
/**
|
||||
* @PostPersist
|
||||
*/
|
||||
@@ -80,5 +80,4 @@ class CompanyContractListener
|
||||
{
|
||||
$this->postLoadCalls[] = func_get_args();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
namespace Doctrine\Tests\ORM\Functional;
|
||||
|
||||
use Doctrine\Tests\Models\Cache\Attraction;
|
||||
use Doctrine\Tests\Models\Cache\AttractionInfo;
|
||||
use Doctrine\Tests\Models\Cache\AttractionContactInfo;
|
||||
use Doctrine\Tests\Models\Cache\AttractionInfo;
|
||||
use Doctrine\Tests\Models\Cache\AttractionLocationInfo;
|
||||
|
||||
/**
|
||||
@@ -188,4 +188,47 @@ class SecondLevelCacheJoinTableInheritanceTest extends SecondLevelCacheAbstractT
|
||||
$this->assertInstanceOf(AttractionContactInfo::CLASSNAME, $entity->getInfos()->get(0));
|
||||
$this->assertEquals($this->attractionsInfo[0]->getFone(), $entity->getInfos()->get(0)->getFone());
|
||||
}
|
||||
}
|
||||
|
||||
public function testQueryCacheShouldBeEvictedOnTimestampUpdate()
|
||||
{
|
||||
$this->loadFixturesCountries();
|
||||
$this->loadFixturesStates();
|
||||
$this->loadFixturesCities();
|
||||
$this->loadFixturesAttractions();
|
||||
$this->loadFixturesAttractionsInfo();
|
||||
$this->evictRegions();
|
||||
$this->_em->clear();
|
||||
|
||||
$queryCount = $this->getCurrentQueryCount();
|
||||
$dql = 'SELECT attractionInfo FROM Doctrine\Tests\Models\Cache\AttractionInfo attractionInfo';
|
||||
|
||||
$result1 = $this->_em->createQuery($dql)
|
||||
->setCacheable(true)
|
||||
->getResult();
|
||||
|
||||
$this->assertCount(count($this->attractionsInfo), $result1);
|
||||
$this->assertEquals($queryCount + 5, $this->getCurrentQueryCount());
|
||||
|
||||
$contact = new AttractionContactInfo(
|
||||
'1234-1234',
|
||||
$this->_em->find(Attraction::CLASSNAME, $this->attractions[5]->getId())
|
||||
);
|
||||
|
||||
$this->_em->persist($contact);
|
||||
$this->_em->flush();
|
||||
$this->_em->clear();
|
||||
|
||||
$queryCount = $this->getCurrentQueryCount();
|
||||
|
||||
$result2 = $this->_em->createQuery($dql)
|
||||
->setCacheable(true)
|
||||
->getResult();
|
||||
|
||||
$this->assertCount(count($this->attractionsInfo) + 1, $result2);
|
||||
$this->assertEquals($queryCount + 6, $this->getCurrentQueryCount());
|
||||
|
||||
foreach ($result2 as $entity) {
|
||||
$this->assertInstanceOf(AttractionInfo::CLASSNAME, $entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
|
||||
namespace Doctrine\Tests\ORM\Functional;
|
||||
|
||||
use Doctrine\Tests\Models\Cache\City;
|
||||
use Doctrine\Tests\Models\Cache\ComplexAction;
|
||||
use Doctrine\Tests\Models\Cache\Country;
|
||||
use Doctrine\Tests\Models\Cache\State;
|
||||
use Doctrine\ORM\Mapping\ClassMetadata;
|
||||
use Doctrine\Tests\Models\Cache\Token;
|
||||
use Doctrine\Tests\Models\Cache\Action;
|
||||
|
||||
@@ -98,6 +98,40 @@ class SecondLevelCacheManyToOneTest extends SecondLevelCacheAbstractTest
|
||||
$this->assertEquals($this->states[1]->getCountry()->getName(), $c4->getCountry()->getName());
|
||||
}
|
||||
|
||||
public function testInverseSidePutShouldEvictCollection()
|
||||
{
|
||||
$this->loadFixturesCountries();
|
||||
$this->loadFixturesStates();
|
||||
|
||||
$this->_em->clear();
|
||||
|
||||
$this->cache->evictEntityRegion(State::CLASSNAME);
|
||||
$this->cache->evictEntityRegion(Country::CLASSNAME);
|
||||
|
||||
//evict collection on add
|
||||
$c3 = $this->_em->find(State::CLASSNAME, $this->states[0]->getId());
|
||||
$prev = $c3->getCities();
|
||||
$count = $prev->count();
|
||||
$city = new City("Buenos Aires", $c3);
|
||||
|
||||
$c3->addCity($city);
|
||||
|
||||
$this->_em->persist($city);
|
||||
$this->_em->persist($c3);
|
||||
$this->_em->flush();
|
||||
$this->_em->clear();
|
||||
|
||||
$state = $this->_em->find(State::CLASSNAME, $c3->getId());
|
||||
$queryCount = $this->getCurrentQueryCount();
|
||||
|
||||
// Association was cleared from EM
|
||||
$this->assertNotEquals($prev, $state->getCities());
|
||||
|
||||
// New association has one more item (cache was evicted)
|
||||
$this->assertEquals($count + 1, $state->getCities()->count());
|
||||
$this->assertEquals($queryCount, $this->getCurrentQueryCount());
|
||||
}
|
||||
|
||||
public function testShouldNotReloadWhenAssociationIsMissing()
|
||||
{
|
||||
$this->loadFixturesCountries();
|
||||
|
||||
@@ -14,18 +14,18 @@ use Doctrine\Tests\Models\Cache\Traveler;
|
||||
*/
|
||||
class SecondLevelCacheOneToManyTest extends SecondLevelCacheAbstractTest
|
||||
{
|
||||
public function testShouldNotPutCollectionInverseSideOnPersist()
|
||||
public function testShouldPutCollectionInverseSideOnPersist()
|
||||
{
|
||||
$this->loadFixturesCountries();
|
||||
$this->loadFixturesStates();
|
||||
$this->loadFixturesCities();
|
||||
|
||||
$this->_em->clear();
|
||||
|
||||
$this->assertTrue($this->cache->containsEntity(State::CLASSNAME, $this->states[0]->getId()));
|
||||
$this->assertTrue($this->cache->containsEntity(State::CLASSNAME, $this->states[1]->getId()));
|
||||
|
||||
$this->assertFalse($this->cache->containsCollection(State::CLASSNAME, 'cities', $this->states[0]->getId()));
|
||||
$this->assertFalse($this->cache->containsCollection(State::CLASSNAME, 'cities', $this->states[1]->getId()));
|
||||
$this->assertTrue($this->cache->containsCollection(State::CLASSNAME, 'cities', $this->states[0]->getId()));
|
||||
$this->assertTrue($this->cache->containsCollection(State::CLASSNAME, 'cities', $this->states[1]->getId()));
|
||||
}
|
||||
|
||||
public function testPutAndLoadOneToManyRelation()
|
||||
@@ -187,6 +187,7 @@ class SecondLevelCacheOneToManyTest extends SecondLevelCacheAbstractTest
|
||||
$this->loadFixturesCountries();
|
||||
$this->loadFixturesStates();
|
||||
$this->loadFixturesCities();
|
||||
|
||||
$this->_em->clear();
|
||||
$this->secondLevelCacheLogger->clearStats();
|
||||
|
||||
@@ -247,8 +248,8 @@ class SecondLevelCacheOneToManyTest extends SecondLevelCacheAbstractTest
|
||||
$this->_em->remove($city0);
|
||||
$this->_em->persist($state);
|
||||
$this->_em->flush();
|
||||
|
||||
$this->_em->clear();
|
||||
|
||||
$this->secondLevelCacheLogger->clearStats();
|
||||
|
||||
$queryCount = $this->getCurrentQueryCount();
|
||||
@@ -261,19 +262,19 @@ class SecondLevelCacheOneToManyTest extends SecondLevelCacheAbstractTest
|
||||
$this->assertInstanceOf(City::CLASSNAME, $city1);
|
||||
$this->assertEquals($entity->getCities()->get(1)->getName(), $city1->getName());
|
||||
|
||||
$this->assertEquals(1, $this->secondLevelCacheLogger->getHitCount());
|
||||
$this->assertEquals(2, $this->secondLevelCacheLogger->getHitCount());
|
||||
$this->assertEquals(1, $this->secondLevelCacheLogger->getRegionHitCount($this->getEntityRegion(State::CLASSNAME)));
|
||||
$this->assertEquals(0, $this->secondLevelCacheLogger->getRegionHitCount($this->getCollectionRegion(State::CLASSNAME, 'cities')));
|
||||
$this->assertEquals(1, $this->secondLevelCacheLogger->getRegionHitCount($this->getCollectionRegion(State::CLASSNAME, 'cities')));
|
||||
|
||||
$this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
|
||||
$this->assertEquals($queryCount, $this->getCurrentQueryCount());
|
||||
|
||||
$state->getCities()->remove(0);
|
||||
|
||||
$this->_em->remove($city1);
|
||||
$this->_em->persist($state);
|
||||
$this->_em->flush();
|
||||
|
||||
$this->_em->clear();
|
||||
|
||||
$this->secondLevelCacheLogger->clearStats();
|
||||
|
||||
$queryCount = $this->getCurrentQueryCount();
|
||||
@@ -281,9 +282,9 @@ class SecondLevelCacheOneToManyTest extends SecondLevelCacheAbstractTest
|
||||
|
||||
$this->assertCount(0, $state->getCities());
|
||||
|
||||
$this->assertEquals(1, $this->secondLevelCacheLogger->getHitCount());
|
||||
$this->assertEquals(2, $this->secondLevelCacheLogger->getHitCount());
|
||||
$this->assertEquals(1, $this->secondLevelCacheLogger->getRegionHitCount($this->getEntityRegion(State::CLASSNAME)));
|
||||
$this->assertEquals(0, $this->secondLevelCacheLogger->getRegionHitCount($this->getCollectionRegion(State::CLASSNAME, 'cities')));
|
||||
$this->assertEquals(1, $this->secondLevelCacheLogger->getRegionHitCount($this->getCollectionRegion(State::CLASSNAME, 'cities')));
|
||||
}
|
||||
|
||||
public function testOneToManyWithEmptyRelation()
|
||||
@@ -346,11 +347,12 @@ class SecondLevelCacheOneToManyTest extends SecondLevelCacheAbstractTest
|
||||
public function testCacheInitializeCollectionWithNewObjects()
|
||||
{
|
||||
$this->_em->clear();
|
||||
|
||||
$this->evictRegions();
|
||||
|
||||
$traveler = new Traveler("Doctrine Bot");
|
||||
|
||||
for ($i=0; $i<3; ++$i) {
|
||||
for ($i = 0; $i < 3; ++$i) {
|
||||
$traveler->getTravels()->add(new Travel($traveler));
|
||||
}
|
||||
|
||||
@@ -373,7 +375,7 @@ class SecondLevelCacheOneToManyTest extends SecondLevelCacheAbstractTest
|
||||
$this->assertFalse($entity->getTravels()->isInitialized());
|
||||
$this->assertCount(4, $entity->getTravels());
|
||||
$this->assertTrue($entity->getTravels()->isInitialized());
|
||||
$this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
|
||||
$this->assertEquals($queryCount, $this->getCurrentQueryCount());
|
||||
|
||||
$this->_em->flush();
|
||||
$this->_em->clear();
|
||||
|
||||
@@ -210,4 +210,45 @@ class SecondLevelCacheSingleTableInheritanceTest extends SecondLevelCacheAbstrac
|
||||
$this->assertEquals($this->attractions[0]->getName(), $entity->getAttractions()->get(0)->getName());
|
||||
$this->assertEquals($this->attractions[1]->getName(), $entity->getAttractions()->get(1)->getName());
|
||||
}
|
||||
}
|
||||
|
||||
public function testQueryCacheShouldBeEvictedOnTimestampUpdate()
|
||||
{
|
||||
$this->loadFixturesCountries();
|
||||
$this->loadFixturesStates();
|
||||
$this->loadFixturesCities();
|
||||
$this->loadFixturesAttractions();
|
||||
$this->_em->clear();
|
||||
|
||||
$queryCount = $this->getCurrentQueryCount();
|
||||
$dql = 'SELECT attraction FROM Doctrine\Tests\Models\Cache\Attraction attraction';
|
||||
|
||||
$result1 = $this->_em->createQuery($dql)
|
||||
->setCacheable(true)
|
||||
->getResult();
|
||||
|
||||
$this->assertCount(count($this->attractions), $result1);
|
||||
$this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
|
||||
|
||||
$contact = new Beach(
|
||||
'Botafogo',
|
||||
$this->_em->find(City::CLASSNAME, $this->cities[1]->getId())
|
||||
);
|
||||
|
||||
$this->_em->persist($contact);
|
||||
$this->_em->flush();
|
||||
$this->_em->clear();
|
||||
|
||||
$queryCount = $this->getCurrentQueryCount();
|
||||
|
||||
$result2 = $this->_em->createQuery($dql)
|
||||
->setCacheable(true)
|
||||
->getResult();
|
||||
|
||||
$this->assertCount(count($this->attractions) + 1, $result2);
|
||||
$this->assertEquals($queryCount + 1, $this->getCurrentQueryCount());
|
||||
|
||||
foreach ($result2 as $entity) {
|
||||
$this->assertInstanceOf(Attraction::CLASSNAME, $entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
namespace Doctrine\Tests\ORM;
|
||||
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Doctrine\ORM\PersistentCollection;
|
||||
use Doctrine\Tests\Mocks\ConnectionMock;
|
||||
use Doctrine\Tests\Mocks\DriverMock;
|
||||
@@ -83,4 +82,44 @@ class PersistentCollectionTest extends OrmTestCase
|
||||
$this->collection->next();
|
||||
$this->assertTrue($this->collection->isInitialized());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group 6110
|
||||
*/
|
||||
public function testRemovingElementsAlsoRemovesKeys()
|
||||
{
|
||||
$this->setUpPersistentCollection();
|
||||
|
||||
$this->collection->add('dummy');
|
||||
$this->assertEquals([0], array_keys($this->collection->toArray()));
|
||||
|
||||
$this->collection->removeElement('dummy');
|
||||
$this->assertEquals([], array_keys($this->collection->toArray()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @group 6110
|
||||
*/
|
||||
public function testClearWillAlsoClearKeys()
|
||||
{
|
||||
$this->setUpPersistentCollection();
|
||||
|
||||
$this->collection->add('dummy');
|
||||
$this->collection->clear();
|
||||
$this->assertEquals([], array_keys($this->collection->toArray()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @group 6110
|
||||
*/
|
||||
public function testClearWillAlsoResetKeyPositions()
|
||||
{
|
||||
$this->setUpPersistentCollection();
|
||||
|
||||
$this->collection->add('dummy');
|
||||
$this->collection->removeElement('dummy');
|
||||
$this->collection->clear();
|
||||
$this->collection->add('dummy');
|
||||
$this->assertEquals([0], array_keys($this->collection->toArray()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,8 +3,11 @@
|
||||
namespace Doctrine\Tests\ORM;
|
||||
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\EventManager;
|
||||
use Doctrine\Common\NotifyPropertyChanged;
|
||||
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
|
||||
use Doctrine\Common\PropertyChangedListener;
|
||||
use Doctrine\ORM\Events;
|
||||
use Doctrine\ORM\Mapping\ClassMetadata;
|
||||
use Doctrine\ORM\UnitOfWork;
|
||||
use Doctrine\Tests\Mocks\ConnectionMock;
|
||||
@@ -47,18 +50,22 @@ class UnitOfWorkTest extends \Doctrine\Tests\OrmTestCase
|
||||
*/
|
||||
private $_emMock;
|
||||
|
||||
protected function setUp() {
|
||||
/**
|
||||
* @var EventManager|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
private $eventManager;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->_connectionMock = new ConnectionMock(array(), new DriverMock());
|
||||
$this->_emMock = EntityManagerMock::create($this->_connectionMock);
|
||||
$this->_connectionMock = new ConnectionMock([], new DriverMock());
|
||||
$this->eventManager = $this->getMockBuilder('Doctrine\Common\EventManager')->getMock();
|
||||
$this->_emMock = EntityManagerMock::create($this->_connectionMock, null, $this->eventManager);
|
||||
// SUT
|
||||
$this->_unitOfWork = new UnitOfWorkMock($this->_emMock);
|
||||
$this->_emMock->setUnitOfWork($this->_unitOfWork);
|
||||
}
|
||||
|
||||
protected function tearDown() {
|
||||
}
|
||||
|
||||
public function testRegisterRemovedOnNewEntityIsIgnored()
|
||||
{
|
||||
$user = new ForumUser();
|
||||
@@ -392,6 +399,88 @@ class UnitOfWorkTest extends \Doctrine\Tests\OrmTestCase
|
||||
|
||||
self::assertSame([], $this->_unitOfWork->getOriginalEntityData($newUser), 'No original data was stored');
|
||||
}
|
||||
|
||||
/**
|
||||
* @group DDC-1955
|
||||
* @group 5570
|
||||
* @group 6174
|
||||
*/
|
||||
public function testMergeWithNewEntityWillPersistItAndTriggerPrePersistListenersWithMergedEntityData()
|
||||
{
|
||||
$entity = new EntityWithRandomlyGeneratedField();
|
||||
|
||||
$generatedFieldValue = $entity->generatedField;
|
||||
|
||||
$this
|
||||
->eventManager
|
||||
->expects(self::any())
|
||||
->method('hasListeners')
|
||||
->willReturnCallback(function ($eventName) {
|
||||
return $eventName === Events::prePersist;
|
||||
});
|
||||
$this
|
||||
->eventManager
|
||||
->expects(self::once())
|
||||
->method('dispatchEvent')
|
||||
->with(
|
||||
self::anything(),
|
||||
self::callback(function (LifecycleEventArgs $args) use ($entity, $generatedFieldValue) {
|
||||
/* @var $object EntityWithRandomlyGeneratedField */
|
||||
$object = $args->getObject();
|
||||
|
||||
self::assertInstanceOf('Doctrine\Tests\ORM\EntityWithRandomlyGeneratedField', $object);
|
||||
self::assertNotSame($entity, $object);
|
||||
self::assertSame($generatedFieldValue, $object->generatedField);
|
||||
|
||||
return true;
|
||||
})
|
||||
);
|
||||
|
||||
/* @var $object EntityWithRandomlyGeneratedField */
|
||||
$object = $this->_unitOfWork->merge($entity);
|
||||
|
||||
self::assertNotSame($object, $entity);
|
||||
self::assertInstanceOf('Doctrine\Tests\ORM\EntityWithRandomlyGeneratedField', $object);
|
||||
self::assertSame($object->generatedField, $entity->generatedField);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group DDC-1955
|
||||
* @group 5570
|
||||
* @group 6174
|
||||
*/
|
||||
public function testMergeWithExistingEntityWillNotPersistItNorTriggerPrePersistListeners()
|
||||
{
|
||||
$persistedEntity = new EntityWithRandomlyGeneratedField();
|
||||
$mergedEntity = new EntityWithRandomlyGeneratedField();
|
||||
|
||||
$mergedEntity->id = $persistedEntity->id;
|
||||
$mergedEntity->generatedField = mt_rand(
|
||||
$persistedEntity->generatedField + 1,
|
||||
$persistedEntity->generatedField + 1000
|
||||
);
|
||||
|
||||
$this
|
||||
->eventManager
|
||||
->expects(self::any())
|
||||
->method('hasListeners')
|
||||
->willReturnCallback(function ($eventName) {
|
||||
return $eventName === Events::prePersist;
|
||||
});
|
||||
$this->eventManager->expects(self::never())->method('dispatchEvent');
|
||||
|
||||
$this->_unitOfWork->registerManaged(
|
||||
$persistedEntity,
|
||||
['id' => $persistedEntity->id],
|
||||
['generatedField' => $persistedEntity->generatedField]
|
||||
);
|
||||
|
||||
/* @var $merged EntityWithRandomlyGeneratedField */
|
||||
$merged = $this->_unitOfWork->merge($mergedEntity);
|
||||
|
||||
self::assertSame($merged, $persistedEntity);
|
||||
self::assertSame($persistedEntity->generatedField, $mergedEntity->generatedField);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -498,3 +587,61 @@ class VersionedAssignedIdentifierEntity
|
||||
*/
|
||||
public $version;
|
||||
}
|
||||
|
||||
/** @Entity */
|
||||
class EntityWithStringIdentifier
|
||||
{
|
||||
/**
|
||||
* @Id @Column(type="string")
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
public $id;
|
||||
}
|
||||
|
||||
/** @Entity */
|
||||
class EntityWithBooleanIdentifier
|
||||
{
|
||||
/**
|
||||
* @Id @Column(type="boolean")
|
||||
*
|
||||
* @var bool|null
|
||||
*/
|
||||
public $id;
|
||||
}
|
||||
|
||||
/** @Entity */
|
||||
class EntityWithCompositeStringIdentifier
|
||||
{
|
||||
/**
|
||||
* @Id @Column(type="string")
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
public $id1;
|
||||
|
||||
/**
|
||||
* @Id @Column(type="string")
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
public $id2;
|
||||
}
|
||||
|
||||
/** @Entity */
|
||||
class EntityWithRandomlyGeneratedField
|
||||
{
|
||||
/** @Id @Column(type="string") */
|
||||
public $id;
|
||||
|
||||
/**
|
||||
* @Column(type="integer")
|
||||
*/
|
||||
public $generatedField;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->id = uniqid('id', true);
|
||||
$this->generatedField = mt_rand(0, 100000);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user