Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SLC] Fix cache misses using one-to-one inverse side #884

Merged
merged 1 commit into from
Dec 20, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/Doctrine/ORM/Cache/DefaultEntityHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
namespace Doctrine\ORM\Cache;

use Doctrine\ORM\Query;
use Doctrine\Common\Proxy\Proxy;
use Doctrine\ORM\Cache\EntityCacheKey;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\EntityManagerInterface;
Expand Down Expand Up @@ -133,7 +132,7 @@ public function loadCacheEntry(ClassMetadata $metadata, EntityCacheKey $key, Ent
return null;
}

$data[$name] = $assoc['fetch'] === ClassMetadata::FETCH_EAGER
$data[$name] = $assoc['fetch'] === ClassMetadata::FETCH_EAGER || ($assoc['type'] === ClassMetadata::ONE_TO_ONE && ! $assoc['isOwningSide'])
? $this->uow->createEntity($assocEntry->class, $assocEntry->data, $hints)
: $this->em->getReference($assocEntry->class, $assocId);
}
Expand Down
47 changes: 47 additions & 0 deletions lib/Doctrine/ORM/Cache/Persister/AbstractEntityPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ abstract class AbstractEntityPersister implements CachedEntityPersister
*/
protected $regionName;

/**
* Associations configured as FETCH_EAGER, as well as all inverse one-to-one associations.
*
* @var array
*/
protected $joinedAssociations;

/**
* @param \Doctrine\ORM\Persisters\EntityPersister $persister The entity persister to cache.
* @param \Doctrine\ORM\Cache\Region $region The entity cache region.
Expand Down Expand Up @@ -227,6 +234,42 @@ public function storeEntityCache($entity, EntityCacheKey $key)
return $cached;
}

/**
* @param object $entity
*/
private function storeJoinedAssociations($entity)
{
if ($this->joinedAssociations === null) {
$associations = array();

foreach ($this->class->associationMappings as $name => $assoc) {
if (isset($assoc['cache']) &&
($assoc['type'] & ClassMetadata::TO_ONE) &&
($assoc['fetch'] === ClassMetadata::FETCH_EAGER || ! $assoc['isOwningSide'])) {

$associations[] = $name;
}
}

$this->joinedAssociations = $associations;
}

foreach ($this->joinedAssociations as $name) {
$assoc = $this->class->associationMappings[$name];
$assocEntity = $this->class->getFieldValue($entity, $name);

if ($assocEntity === null) {
continue;
}

$assocId = $this->uow->getEntityIdentifier($assocEntity);
$assocKey = new EntityCacheKey($assoc['targetEntity'], $assocId);
$assocPersister = $this->uow->getEntityPersister($assoc['targetEntity']);

$assocPersister->storeEntityCache($assocEntity, $assocKey);
}
}

/**
* Generates a string of currently query
*
Expand Down Expand Up @@ -417,6 +460,10 @@ public function loadById(array $identifier, $entity = null)
$cacheEntry = $this->hydrator->buildCacheEntry($class, $cacheKey, $entity);
$cached = $this->region->put($cacheKey, $cacheEntry);

if ($cached && ($this->joinedAssociations === null || count($this->joinedAssociations) > 0)) {
$this->storeJoinedAssociations($entity);
}

if ($this->cacheLogger) {
if ($cached) {
$this->cacheLogger->entityCachePut($this->regionName, $cacheKey);
Expand Down
12 changes: 12 additions & 0 deletions lib/Doctrine/ORM/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -2602,6 +2602,18 @@ public function createEntity($className, array $data, &$hints = array())
switch (true) {
case ($assoc['type'] & ClassMetadata::TO_ONE):
if ( ! $assoc['isOwningSide']) {

// use the given entity association
if (isset($data[$field]) && is_object($data[$field]) && isset($this->entityStates[spl_object_hash($data[$field])])) {

$this->originalEntityData[$oid][$field] = $data[$field];

$class->reflFields[$field]->setValue($entity, $data[$field]);
$targetClass->reflFields[$assoc['mappedBy']]->setValue($data[$field], $entity);

continue 2;
}

// Inverse side of x-to-one can never be lazy
$class->reflFields[$field]->setValue($entity, $this->getEntityPersister($assoc['targetEntity'])->loadOneToOneEntity($assoc, $entity));

Expand Down
2 changes: 1 addition & 1 deletion tests/Doctrine/Tests/Models/Cache/Beach.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
*/
class Beach extends Attraction
{
const CLASSNAME = __CLASS__;
const CLASSNAME = __CLASS__;
}
24 changes: 23 additions & 1 deletion tests/Doctrine/Tests/Models/Cache/Traveler.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ class Traveler
*/
public $travels;

/**
* @Cache
* @OneToOne(targetEntity="TravelerProfile")
*/
protected $profile;

/**
* @param string $name
*/
Expand Down Expand Up @@ -62,6 +68,22 @@ public function setName($name)
$this->name = $name;
}

/**
* @return \Doctrine\Tests\Models\Cache\TravelerProfile
*/
public function getProfile()
{
return $this->profile;
}

/**
* @param \Doctrine\Tests\Models\Cache\TravelerProfile $profile
*/
public function setProfile(TravelerProfile $profile)
{
$this->profile = $profile;
}

public function getTravels()
{
return $this->travels;
Expand All @@ -88,4 +110,4 @@ public function removeTravel(Travel $item)
{
$this->travels->removeElement($item);
}
}
}
66 changes: 66 additions & 0 deletions tests/Doctrine/Tests/Models/Cache/TravelerProfile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Doctrine\Tests\Models\Cache;

/**
* @Entity
* @Table("cache_traveler_profile")
* @Cache("NONSTRICT_READ_WRITE")
*/
class TravelerProfile
{
const CLASSNAME = __CLASS__;

/**
* @Id
* @GeneratedValue
* @Column(type="integer")
*/
protected $id;

/**
* @Column(unique=true)
*/
private $name;

/**
* @OneToOne(targetEntity="TravelerProfileInfo", mappedBy="profile")
* @Cache()
*/
private $info;

public function __construct($name)
{
$this->name = $name;
}

public function getId()
{
return $this->id;
}

public function setId($id)
{
$this->id = $id;
}

public function getName()
{
return $this->name;
}

public function setName($nae)
{
$this->name = $nae;
}

public function getInfo()
{
return $this->info;
}

public function setInfo(TravelerProfileInfo $info)
{
$this->info = $info;
}
}
68 changes: 68 additions & 0 deletions tests/Doctrine/Tests/Models/Cache/TravelerProfileInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Doctrine\Tests\Models\Cache;

/**
* @Entity
* @Table("cache_traveler_profile_info")
* @Cache("NONSTRICT_READ_WRITE")
*/
class TravelerProfileInfo
{
const CLASSNAME = __CLASS__;

/**
* @Id
* @GeneratedValue
* @Column(type="integer")
*/
protected $id;

/**
* @Column(unique=true)
*/
private $description;

/**
* @Cache()
* @JoinColumn(name="profile_id", referencedColumnName="id")
* @OneToOne(targetEntity="TravelerProfile", inversedBy="info")
*/
private $profile;

public function __construct(TravelerProfile $profile, $description)
{
$this->profile = $profile;
$this->description = $description;
}

public function getId()
{
return $this->id;
}

public function setId($id)
{
$this->id = $id;
}

public function getDescription()
{
return $this->description;
}

public function setDescription($description)
{
$this->description = $description;
}

public function getProfile()
{
return $this->profile;
}

public function setProfile(TravelerProfile $profile)
{
$this->profile = $profile;
}
}
12 changes: 12 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/OneToOneEagerLoadingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ protected function setUp()
} catch(\Exception $e) {}
}

/**
* @group non-cacheable
*/
public function testEagerLoadOneToOneOwningSide()
{
$train = new Train(new TrainOwner("Alexander"));
Expand All @@ -48,6 +51,9 @@ public function testEagerLoadOneToOneOwningSide()
$this->assertEquals($sqlCount + 1, count($this->_sqlLoggerStack->queries));
}

/**
* @group non-cacheable
*/
public function testEagerLoadOneToOneNullOwningSide()
{
$train = new Train(new TrainOwner("Alexander"));
Expand All @@ -65,6 +71,9 @@ public function testEagerLoadOneToOneNullOwningSide()
$this->assertEquals($sqlCount + 1, count($this->_sqlLoggerStack->queries));
}

/**
* @group non-cacheable
*/
public function testEagerLoadOneToOneInverseSide()
{
$owner = new TrainOwner("Alexander");
Expand All @@ -83,6 +92,9 @@ public function testEagerLoadOneToOneInverseSide()
$this->assertEquals($sqlCount + 1, count($this->_sqlLoggerStack->queries));
}

/**
* @group non-cacheable
*/
public function testEagerLoadOneToOneNullInverseSide()
{
$driver = new TrainDriver("Dagny Taggert");
Expand Down
Loading