Added DB Helper.

This commit is contained in:
李铭昕 2019-01-30 11:07:04 +08:00
parent 8bd1e08b1f
commit e8bae00b5e

View File

@ -0,0 +1,53 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://hyperf.org
* @document https://wiki.hyperf.org
* @contact group@hyperf.org
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/
namespace Hyperf\DbConnection;
use Hyperf\Database\ConnectionInterface;
use Hyperf\Framework\ApplicationContext;
use Psr\Container\ContainerInterface;
/**
* DB Helper.
* @method static beginTransaction
* @method static rollback
* @method statuc commit
*/
class Db
{
/**
* @var ContainerInterface
*/
protected $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function __call($name, $arguments)
{
return $this->connection()->{$name}(...$arguments);
}
public static function __callStatic($name, $arguments)
{
$db = ApplicationContext::getContainer()->get(Db::class);
return $db->connection()->{$name}(...$arguments);
}
public function connection($pool = 'default'): ConnectionInterface
{
$resolver = $this->container->get(ConnectionResolver::class);
return $resolver->connection($pool);
}
}