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.
117 lines
2.5 KiB
117 lines
2.5 KiB
7 years ago
|
<?php
|
||
|
|
||
7 years ago
|
namespace Twister\ORM;
|
||
7 years ago
|
|
||
|
/**
|
||
7 years ago
|
* A Repository serves as a repository for entities with generic as well as
|
||
7 years ago
|
* business specific methods for retrieving entities.
|
||
|
*
|
||
|
* This class is designed for inheritance and users can subclass this class to
|
||
|
* write their own repositories with business-specific methods to locate entities.
|
||
|
*
|
||
|
* @author Trevor Herselman <therselman@gmail.com>
|
||
|
*/
|
||
7 years ago
|
class Repository
|
||
7 years ago
|
{
|
||
|
/**
|
||
7 years ago
|
* The database connection used by the EntityManager.
|
||
7 years ago
|
*
|
||
7 years ago
|
* @var \Doctrine\DBAL\Connection
|
||
7 years ago
|
*/
|
||
7 years ago
|
protected $em = null;
|
||
7 years ago
|
|
||
|
|
||
|
public function __construct(Container $c)
|
||
|
{
|
||
|
$this->conn = $c->db;
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* {@inheritDoc}
|
||
|
*/
|
||
|
public function getConnection()
|
||
|
{
|
||
|
return $this->conn;
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* {@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();
|
||
|
}
|
||
|
|
||
|
|
||
7 years ago
|
/**
|
||
|
* Adds support for magic method calls.
|
||
|
*
|
||
|
* @param string $method
|
||
|
* @param array $args
|
||
|
*
|
||
|
* @return mixed The returned value from the resolved method.
|
||
|
*
|
||
|
* @throws ORMException
|
||
|
* @throws \BadMethodCallException If the method called is invalid
|
||
|
*/
|
||
|
public function __call($method, $args)
|
||
|
{
|
||
|
if (0 === strpos($method, 'findBy')) {
|
||
|
return $this->resolveMagicCall('findBy', substr($method, 6), $args);
|
||
|
}
|
||
|
if (0 === strpos($method, 'findOneBy')) {
|
||
|
return $this->resolveMagicCall('findOneBy', substr($method, 9), $args);
|
||
|
}
|
||
|
if (0 === strpos($method, 'countBy')) {
|
||
|
return $this->resolveMagicCall('count', substr($method, 7), $args);
|
||
|
}
|
||
|
throw new \BadMethodCallException(
|
||
|
"Undefined method '$method'. The method name must start with ".
|
||
|
"either findBy, findOneBy or countBy!"
|
||
|
);
|
||
|
}
|
||
|
|
||
7 years ago
|
}
|