Skip to content

Commit ab50705

Browse files
committed
Multiple connections can be established even if moved to Global.
1 parent 4c52dd4 commit ab50705

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

src/Database.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class Database extends QueryBuilder
3737

3838
private PDO $_pdo;
3939

40+
private bool $isGlobal = false;
41+
4042
private static PDO $_globalPDO;
4143

4244
private array $_credentials = [
@@ -75,7 +77,6 @@ class Database extends QueryBuilder
7577
'testMode' => false,
7678
];
7779

78-
7980
private array $_errors = [];
8081

8182
private Validation $_validation;
@@ -97,7 +98,18 @@ public function __call($name, $arguments)
9798

9899
final public function newInstance(array $credentials = []): Database
99100
{
100-
return new self(empty($credentials) ? $this->_credentials : \array_merge($this->_credentials, $credentials));
101+
$instance = new Database(empty($credentials) ? $this->_credentials : \array_merge($this->_credentials, $credentials));
102+
$instance->isGlobal = false;
103+
104+
return $instance;
105+
}
106+
107+
final public function clone(): Database
108+
{
109+
$clone = new self($this->_credentials);
110+
$clone->isGlobal = $this->isGlobal;
111+
112+
return $clone;
101113
}
102114

103115
final public function setCredentials(array $credentials): self
@@ -124,11 +136,12 @@ final public function getError(): array
124136
final public function connectionAsGlobal(): void
125137
{
126138
self::$_globalPDO = $this->getPDO();
139+
$this->isGlobal = true;
127140
}
128141

129142
final public function getPDO(): PDO
130143
{
131-
if(isset(self::$_globalPDO)){
144+
if(isset(self::$_globalPDO) && $this->isGlobal){
132145
return self::$_globalPDO;
133146
}
134147
if(!isset($this->_pdo)){

src/Facade/DB.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,13 @@ public static function createImmutable(array $credentials): Database
158158
return self::$databaseInstance = new Database($credentials);
159159
}
160160

161+
public static function connect(array $credentials): Database
162+
{
163+
return isset(self::$databaseInstance)
164+
? self::getDatabase()->newInstance($credentials)
165+
: self::createImmutable($credentials);
166+
}
167+
161168
private static function getDatabase(): Database
162169
{
163170
return self::$databaseInstance;

src/Helpers/Validation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ protected function is_unique($data, $column, $schemaID = null): bool
147147
if($this->db->getSchemaID() === null){
148148
throw new ValidationException('You need a model with a PRIMARY KEY to use the is_unique validation.');
149149
}
150-
$db = $this->db->newInstance();
150+
$db = $this->db->clone();
151151
$db->reset();
152152
$parameters = Parameters::get(true);
153153
$db->select($db->getSchemaID())->offset(0)

0 commit comments

Comments
 (0)