Skip to content

Commit

Permalink
Finished reorganization of persisters. Moved instantiation to EntityM…
Browse files Browse the repository at this point in the history
…anager.
  • Loading branch information
Guilherme Blanco committed Aug 28, 2013
1 parent 04e3370 commit b8f57b5
Show file tree
Hide file tree
Showing 20 changed files with 383 additions and 149 deletions.
28 changes: 28 additions & 0 deletions lib/Doctrine/ORM/Configuration.php
Expand Up @@ -35,6 +35,8 @@
use Doctrine\ORM\Mapping\QuoteStrategy;
use Doctrine\ORM\Repository\DefaultRepositoryFactory;
use Doctrine\ORM\Repository\RepositoryFactory;
use Doctrine\ORM\Persister\DefaultPersisterFactory;
use Doctrine\ORM\Persister\PersisterFactory;

/**
* Configuration container for all configuration options of Doctrine.
Expand Down Expand Up @@ -805,4 +807,30 @@ public function getRepositoryFactory()
? $this->_attributes['repositoryFactory']
: new DefaultRepositoryFactory();
}

/**
* Set the persister factory.
*
* @since 2.5
*
* @param \Doctrine\ORM\Persister\PersisterFactory $persisterFactory
*/
public function setPersisterFactory(PersisterFactory $persisterFactory)
{
$this->_attributes['persisterFactory'] = $persisterFactory;
}

/**
* Get the persister factory.
*
* @since 2.5
*
* @return \Doctrine\ORM\Persister\PersisterFactory
*/
public function getPersisterFactory()
{
return isset($this->_attributes['persisterFactory'])
? $this->_attributes['persisterFactory']
: new DefaultPersisterFactory();
}
}
16 changes: 16 additions & 0 deletions lib/Doctrine/ORM/Decorator/EntityManagerDecorator.php
Expand Up @@ -149,6 +149,22 @@ public function getPartialReference($entityName, $identifier)
return $this->wrapped->getPartialReference($entityName, $identifier);
}

/**
* {@inheritdoc}
*/
public function getEntityPersister($entityName)
{
return $this->wrapped->getEntityPersister($entityName);
}

/**
* {@inheritdoc}
*/
public function getCollectionPersister($association)
{
return $this->wrapped->getCollectionPersister($association);
}

/**
* {@inheritdoc}
*/
Expand Down
38 changes: 36 additions & 2 deletions lib/Doctrine/ORM/EntityManager.php
Expand Up @@ -113,6 +113,13 @@
*/
private $repositoryFactory;

/**
* The persister factory used to create persisters.
*
* @var \Doctrine\ORM\Persister\PersisterFactory
*/
private $persisterFactory;

/**
* The expression builder instance used to generate query expressions.
*
Expand Down Expand Up @@ -155,6 +162,7 @@ protected function __construct(Connection $conn, Configuration $config, EventMan
$this->metadataFactory->setCacheDriver($this->config->getMetadataCacheImpl());

$this->repositoryFactory = $config->getRepositoryFactory();
$this->persisterFactory = $config->getPersisterFactory();
$this->unitOfWork = new UnitOfWork($this);
$this->proxyFactory = new ProxyFactory(
$this,
Expand Down Expand Up @@ -445,15 +453,15 @@ public function find($entityName, $id, $lockMode = LockMode::NONE, $lockVersion

case LockMode::PESSIMISTIC_READ:
case LockMode::PESSIMISTIC_WRITE:
$persister = $unitOfWork->getEntityPersister($class->name);
$persister = $this->getEntityPersister($class->name);
$persister->refresh($sortedId, $entity, $lockMode);
break;
}

return $entity; // Hit!
}

$persister = $unitOfWork->getEntityPersister($class->name);
$persister = $this->getEntityPersister($class->name);

switch ($lockMode) {
case LockMode::NONE:
Expand Down Expand Up @@ -759,6 +767,32 @@ public function getRepository($entityName)
return $this->repositoryFactory->getRepository($this, $entityName);
}

/**
* Gets the persister for an entity class.
*
* @param string $entityName The name of the entity.
*
* @return \Doctrine\ORM\Persister\Entity\EntityPersister
*/
public function getEntityPersister($entityName)
{
$classMetadata = $this->getClassMetadata($entityName);

return $this->persisterFactory->createEntityPersister($this, $classMetadata);
}

/**
* Gets the persister for a collection.
*
* @param array $association The association mapping.
*
* @return \Doctrine\ORM\Persister\Collection\CollectionPersister
*/
public function getCollectionPersister($association)
{
return $this->persisterFactory->createCollectionPersister($this, $association);
}

/**
* Determines whether an entity instance is managed in this EntityManager.
*
Expand Down
4 changes: 3 additions & 1 deletion lib/Doctrine/ORM/EntityManagerInterface.php
Expand Up @@ -27,7 +27,7 @@
* EntityManager interface
*
* @since 2.4
* @author Lars Strojny <lars@strojny.net
* @author Lars Strojny <lars@strojny.net>
*/
interface EntityManagerInterface extends ObjectManager
{
Expand All @@ -44,6 +44,8 @@ public function createNamedNativeQuery($name);
public function createQueryBuilder();
public function getReference($entityName, $id);
public function getPartialReference($entityName, $identifier);
public function getEntityPersister($entityName);
public function getCollectionPersister($association);
public function close();
public function copy($entity, $deep = false);
public function lock($entity, $lockMode, $lockVersion = null);
Expand Down
6 changes: 3 additions & 3 deletions lib/Doctrine/ORM/EntityRepository.php
Expand Up @@ -176,7 +176,7 @@ public function findAll()
*/
public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
{
$persister = $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName);
$persister = $this->_em->getEntityPersister($this->_entityName);

return $persister->loadAll($criteria, $orderBy, $limit, $offset);
}
Expand All @@ -191,7 +191,7 @@ public function findBy(array $criteria, array $orderBy = null, $limit = null, $o
*/
public function findOneBy(array $criteria, array $orderBy = null)
{
$persister = $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName);
$persister = $this->_em->getEntityPersister($this->_entityName);

return $persister->load($criteria, null, null, array(), 0, 1, $orderBy);
}
Expand Down Expand Up @@ -299,7 +299,7 @@ protected function getClassMetadata()
*/
public function matching(Criteria $criteria)
{
$persister = $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName);
$persister = $this->_em->getEntityPersister($this->_entityName);

return new ArrayCollection($persister->loadCriteria($criteria));
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/ORM/PersistentCollection.php
Expand Up @@ -778,7 +778,7 @@ public function current()
public function next()
{
$this->initialize();

return $this->coll->next();
}

Expand Down Expand Up @@ -875,7 +875,7 @@ public function matching(Criteria $criteria)

$criteria->where($expression);

$persister = $this->em->getUnitOfWork()->getEntityPersister($this->association['targetEntity']);
$persister = $this->em->getEntityPersister($this->association['targetEntity']);

return new ArrayCollection($persister->loadCriteria($criteria));
}
Expand Down
Expand Up @@ -26,9 +26,11 @@
* Base class for all collection persisters.
*
* @since 2.0
*
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Roman Borschel <roman@code-factory.org>
*/
abstract class AbstractCollectionPersister
abstract class AbstractCollectionPersister implements CollectionPersister
{
/**
* @var EntityManager
Expand Down
Expand Up @@ -265,9 +265,10 @@ public function count(PersistentCollection $coll)
*/
public function slice(PersistentCollection $coll, $offset, $length = null)
{
$mapping = $coll->getMapping();
$mapping = $coll->getMapping();
$persister = $this->em->getEntityPersister($mapping['targetEntity']);

return $this->em->getUnitOfWork()->getEntityPersister($mapping['targetEntity'])->getManyToManyCollection($mapping, $coll->getOwner(), $offset, $length);
return $persister->getManyToManyCollection($mapping, $coll->getOwner(), $offset, $length);
}

/**
Expand Down
Expand Up @@ -40,14 +40,25 @@ class OneToManyCollectionPersister extends AbstractCollectionPersister
public function get(PersistentCollection $coll, $index)
{
$mapping = $coll->getMapping();
$uow = $this->em->getUnitOfWork();
$persister = $uow->getEntityPersister($mapping['targetEntity']);
$persister = $this->em->getEntityPersister($mapping['targetEntity']);

if (!isset($mapping['indexBy'])) {
throw new \BadMethodCallException("Selecting a collection by index is only supported on indexed collections.");
if ( ! isset($mapping['indexBy'])) {
throw new \BadMethodCallException(
"Selecting a collection by index is only supported on indexed collections."
);
}

return $persister->load(array($mapping['mappedBy'] => $coll->getOwner(), $mapping['indexBy'] => $index), null, null, array(), 0, 1);
return $persister->load(
array(
$mapping['mappedBy'] => $coll->getOwner(),
$mapping['indexBy'] => $index
),
null,
null,
array(),
0,
1
);
}

/**
Expand Down Expand Up @@ -175,8 +186,7 @@ public function count(PersistentCollection $coll)
public function slice(PersistentCollection $coll, $offset, $length = null)
{
$mapping = $coll->getMapping();
$uow = $this->em->getUnitOfWork();
$persister = $uow->getEntityPersister($mapping['targetEntity']);
$persister = $this->em->getEntityPersister($mapping['targetEntity']);

return $persister->getOneToManyCollection($mapping, $coll->getOwner(), $offset, $length);
}
Expand Down Expand Up @@ -204,12 +214,12 @@ public function contains(PersistentCollection $coll, $element)
return false;
}

$persister = $uow->getEntityPersister($mapping['targetEntity']);
$persister = $this->em->getEntityPersister($mapping['targetEntity']);

// only works with single id identifier entities. Will throw an
// exception in Entity Persisters if that is not the case for the
// 'mappedBy' field.
$id = current( $uow->getEntityIdentifier($coll->getOwner()));
$id = current($uow->getEntityIdentifier($coll->getOwner()));

return $persister->exists($element, array($mapping['mappedBy'] => $id));
}
Expand Down

0 comments on commit b8f57b5

Please sign in to comment.