mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-12-03 12:17:48 +08:00
Added DB Helper.
This commit is contained in:
parent
8bd1e08b1f
commit
e8bae00b5e
53
src/db-connection/src/Db.php
Normal file
53
src/db-connection/src/Db.php
Normal 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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user