1
0
mirror of synced 2024-12-05 03:06:05 +03:00

Handled one-to-many non cache-able relations

This commit is contained in:
Asmir Mustafic 2015-02-12 00:27:25 +01:00
parent a2461d6d5f
commit 60164931b8
5 changed files with 129 additions and 2 deletions

View File

@ -81,15 +81,20 @@ class DefaultEntityHydrator implements EntityHydrator
continue;
}
if ( ! isset($assoc['cache']) || ! ($assoc['type'] & ClassMetadata::TO_ONE)) {
$targetClassMetadata = $this->em->getClassMetadata($assoc['targetEntity']);
if (! ($assoc['type'] & ClassMetadata::TO_ONE)) {
unset($data[$name]);
continue;
}
if ( ! isset($assoc['cache'])) {
$targetClassMetadata = $this->em->getClassMetadata($assoc['targetEntity']);
$associationIds = $this->identifierFlattener->flattenIdentifier($targetClassMetadata, $targetClassMetadata->getIdentifierValues($data[$name]));
unset($data[$name]);
foreach ($associationIds as $fieldName => $fieldValue) {
$data[$assoc['targetToSourceKeyColumns'][$targetClassMetadata->getColumnName($fieldName)]] = $fieldValue;
}
continue;
}

View File

@ -0,0 +1,63 @@
<?php
namespace Doctrine\Tests\Models\Cache;
/**
* @Entity
* @Table("cache_login")
*/
class Login
{
const CLASSNAME = __CLASS__;
/**
* @Id
* @GeneratedValue
* @Column(type="integer")
*/
protected $id;
/**
* @Column
*/
private $name;
/**
* @ManyToOne(targetEntity="Token", cascade={"persist", "remove"}, inversedBy="logins")
* @JoinColumn(name="token_id", referencedColumnName="token")
*/
private $token;
public function __construct($name)
{
$this->name = $name;
}
/**
* @param Token $token
*/
public function setToken(Token $token)
{
$this->token = $token;
}
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;
}
}

View File

@ -1,6 +1,7 @@
<?php
namespace Doctrine\Tests\Models\Cache;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @Entity
@ -27,13 +28,37 @@ class Token
*/
protected $client;
/**
* @OneToMany(targetEntity="Login", cascade={"persist", "remove"}, mappedBy="token")
* @var array
*/
protected $logins;
public function __construct($token, Client $client = null)
{
$this->token = $token;
$this->logins = new ArrayCollection();
$this->client = $client;
$this->expiresAt = new \DateTime(date('Y-m-d H:i:s', strtotime("+7 day")));
}
/**
* @return array
*/
public function getLogins()
{
return $this->logins;
}
/**
* @param Login $login
*/
public function addLogin(Login $login)
{
$this->logins[] = $login;
$login->setToken($this);
}
public function getToken()
{
return $this->token;

View File

@ -3,7 +3,9 @@
namespace Doctrine\Tests\ORM\Functional;
use Doctrine\Tests\Models\Cache\City;
use Doctrine\Tests\Models\Cache\Login;
use Doctrine\Tests\Models\Cache\State;
use Doctrine\Tests\Models\Cache\Token;
use Doctrine\Tests\Models\Cache\Travel;
use Doctrine\Tests\Models\Cache\Traveler;
@ -381,4 +383,34 @@ class SecondLevelCacheOneToManyTest extends SecondLevelCacheAbstractTest
$this->assertEquals(4, $result->getTravels()->count());
}
public function testPutAndLoadNonCacheableOneToMany()
{
$this->assertNull($this->cache->getEntityCacheRegion(Login::CLASSNAME));
$this->assertInstanceOf('Doctrine\ORM\Cache\Region', $this->cache->getEntityCacheRegion(Token::CLASSNAME));
$l1 = new Login('session1');
$l2 = new Login('session2');
$token = new Token('token-hash');
$this->_em->persist($token);
$this->_em->flush();
$token->addLogin($l1);
$token->addLogin($l2);
$this->_em->flush();
$this->_em->clear();
$queryCount = $this->getCurrentQueryCount();
$this->assertTrue($this->cache->containsEntity(Token::CLASSNAME, $token->getToken()));
$entity = $this->_em->find(Token::CLASSNAME, $token->getToken());
$this->assertInstanceOf(Token::CLASSNAME, $entity);
$this->assertEquals('token-hash', $entity->getToken());
$this->assertEquals($queryCount, $this->getCurrentQueryCount());
$this->assertCount(2, $entity->getLogins());
$this->assertEquals($queryCount + 1 , $this->getCurrentQueryCount());
}
}

View File

@ -181,6 +181,7 @@ abstract class OrmFunctionalTestCase extends OrmTestCase
'Doctrine\Tests\Models\Cache\Bar',
'Doctrine\Tests\Models\Cache\Flight',
'Doctrine\Tests\Models\Cache\Token',
'Doctrine\Tests\Models\Cache\Login',
'Doctrine\Tests\Models\Cache\Client',
'Doctrine\Tests\Models\Cache\AttractionInfo',
'Doctrine\Tests\Models\Cache\AttractionContactInfo',
@ -417,6 +418,7 @@ abstract class OrmFunctionalTestCase extends OrmTestCase
$conn->executeUpdate('DELETE FROM cache_state');
$conn->executeUpdate('DELETE FROM cache_country');
$conn->executeUpdate('DELETE FROM cache_token');
$conn->executeUpdate('DELETE FROM cache_login');
$conn->executeUpdate('DELETE FROM cache_client');
}