Skip to content

Instantly share code, notes, and snippets.

@pavelkucera
Created January 24, 2012 10:38
Show Gist options
  • Save pavelkucera/1669551 to your computer and use it in GitHub Desktop.
Save pavelkucera/1669551 to your computer and use it in GitHub Desktop.
<?php
namespace Nodlehs\Doctrine;
/**
* @author Pavel Kučera
*/
class EntityManager extends \Doctrine\ORM\EntityManager
{
/************************************************************************************
* Workaround for closing
*/
/** @var bool */
protected $closed = FALSE;
/**
* @param bool
*/
public function close($doIt = FALSE)
{
$this->clear(); // You probably can remove this line, but be aware of the mess which can be in your entities
if ($doIt) {
$this->closed = TRUE;
parent::close();
}
}
/**
* @see Doctrine\ORM\EntityManager::errorIfClosed
*/
public function errorIfClosed()
{
if ($this->closed) {
parent::errorIfClosed();
}
}
/************************************************************************************
* Factory
*/
/**
* @param array|\Doctrine\DBAL\Connection
* @param \Doctrine\ORM\Configuration
* @param \Doctrine\ORM\EventManager
* @return EntityManager
*/
public static function create($conn, \Doctrine\ORM\Configuration $config, \Doctrine\Common\EventManager $eventManager = NULL)
{
if (!$config->getMetadataDriverImpl()) {
throw \Doctrine\ORM\ORMException::missingMappingDriverImpl();
}
switch (true) {
case (is_array($conn)):
$conn = \Doctrine\DBAL\DriverManager::getConnection(
$conn, $config, ($eventManager ? : new \Doctrine\ORM\EventManager)
);
break;
case ($conn instanceof \Doctrine\DBAL\Connection):
if ($eventManager !== null && $conn->getEventManager() !== $eventManager) {
throw \Doctrine\ORM\ORMException::mismatchedEventManager();
}
break;
default:
throw new \InvalidArgumentException("Invalid argument: " . $conn);
}
return new EntityManager($conn, $config, $conn->getEventManager());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment