A fast and light-weight component library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
1.5 KiB

7 years ago
<?php
7 years ago
namespace Twister\ORM;
class EntityManager
7 years ago
{
/**
* The database connection used by the EntityManager.
*
* @var \Doctrine\DBAL\Connection
*/
protected $conn = null;
public function __construct(Container $c)
{
$this->conn = $c->db;
}
/**
* {@inheritDoc}
*/
public function getConnection()
{
return $this->conn;
}
/**
* {@inheritDoc}
*/
public function getRepository($entityName)
{
static $repos = null;
if ( ! isset($repos[$entityName])) {
$repoName = $entityName . 'Repository';
echo 'loading: ' . $repoName;
$repos[$entityName] = new $repoName($this);
}
return $repos[$entityName];
}
/**
* {@inheritDoc}
*/
public function beginTransaction()
{
$this->conn->beginTransaction();
}
/**
* {@inheritDoc}
*/
public function transaction($func) // AKA `transactional` in Doctrine
{
if (!is_callable($func)) {
throw new \InvalidArgumentException('Expected argument of type "callable", got "' . gettype($func) . '"');
}
$this->conn->beginTransaction();
try {
$return = call_user_func($func, $this);
$this->flush();
$this->conn->commit();
return $return ?: true;
} catch (Exception $e) {
$this->close();
$this->conn->rollBack();
throw $e;
}
}
/**
* {@inheritDoc}
*/
public function commit()
{
$this->conn->commit();
}
/**
* {@inheritDoc}
*/
public function rollback()
{
$this->conn->rollBack();
}
}