Skip to content

Commit 6ef42d9

Browse files
committed
fix: table prefix mutation
1 parent edb25e2 commit 6ef42d9

File tree

4 files changed

+49
-19
lines changed

4 files changed

+49
-19
lines changed

src/Blueprint.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@ public function __call($method, $parameters)
129129
throw new RuntimeException("Undefined method [ {$method} ] called on Blueprint");
130130
}
131131

132+
public function withPrefix($prefix)
133+
{
134+
$this->table = "{$prefix}{$this->table}";
135+
136+
return $this;
137+
}
138+
132139
public function build()
133140
{
134141
return $this->toSql();
@@ -730,7 +737,8 @@ private function addIndexQuery()
730737
$query .= ' ADD ';
731738
}
732739

733-
$query .= (isset($indexColumn['type']) ? $indexColumn['type'] : null
740+
$query .= (
741+
isset($indexColumn['type']) ? $indexColumn['type'] : null
734742
) . "INDEX {$indexColumn['name']}_INDEX ({$indexColumn['name']} ASC)";
735743
}
736744

src/Model.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ abstract class Model implements ArrayAccess, JsonSerializable
115115
*/
116116
public function __construct($attributes = [])
117117
{
118-
$this->prefix = Connection::getPrefix();
119118
if (!property_exists($this, 'table') || !$this->table) {
120119
$this->_tableWithoutPrefix = ltrim(
121120
strtolower(
@@ -127,9 +126,17 @@ public function __construct($attributes = [])
127126
),
128127
'_'
129128
) . 's';
129+
} else {
130+
$this->_tableWithoutPrefix = $this->table;
131+
}
132+
133+
$dbPrefix = Connection::wpPrefix();
134+
135+
if ($this->prefix === '') {
136+
$dbPrefix = Connection::getPrefix();
130137
}
131138

132-
$this->table = $this->prefix . $this->_tableWithoutPrefix . $this->table;
139+
$this->table = $dbPrefix . $this->prefix . $this->_tableWithoutPrefix;
133140

134141
if (!isset($this->primaryKey)) {
135142
$this->primaryKey = 'id';
@@ -234,7 +241,8 @@ public function setAttribute($key, $value)
234241
{
235242
if (
236243
$this->_isExists
237-
&& (!isset($this->_original[$key])
244+
&& (
245+
!isset($this->_original[$key])
238246
|| isset($this->_original[$key]) && $this->_original[$key] != $value
239247
)
240248
) {

src/QueryBuilder.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010

1111
class QueryBuilder
1212
{
13-
const UPDATE = 'Update';
13+
public const UPDATE = 'Update';
1414

15-
const INSERT = 'Insert';
15+
public const INSERT = 'Insert';
1616

17-
const DELETE = 'Delete';
17+
public const DELETE = 'Delete';
1818

19-
const SELECT = 'Select';
19+
public const SELECT = 'Select';
2020

21-
const TIME_FORMAT = 'Y-m-d H:i:s';
21+
public const TIME_FORMAT = 'Y-m-d H:i:s';
2222

2323
protected $table;
2424

@@ -541,7 +541,7 @@ public function orHaving(...$params)
541541
*/
542542
public function join($table, $firstColumn, $operator = null, $secondColumn = null, $type = 'INNER')
543543
{
544-
$table = Connection::getPrefix() . $table;
544+
$table = Connection::wpPrefix() . $this->_model->getPrefix() . $table;
545545
$hasAlias = preg_split('/ as /i', $table);
546546
if ($hasAlias && isset($hasAlias[1])) {
547547
$table = $hasAlias[0];
@@ -1330,9 +1330,10 @@ function ($value) {
13301330

13311331
if (
13321332
!empty($ids)
1333-
&& ($allRows = $this->newQuery()
1334-
->where($this->_model->getPrimaryKey(), $ids)
1335-
->get()
1333+
&& (
1334+
$allRows = $this->newQuery()
1335+
->where($this->_model->getPrimaryKey(), $ids)
1336+
->get()
13361337
)
13371338
) {
13381339
return $allRows;

src/Schema.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,21 @@
1010

1111
class Schema
1212
{
13+
public $prefix;
14+
1315
public static function __callStatic($method, $parameters)
1416
{
17+
return (new self())->{$method}(...$parameters);
18+
}
19+
20+
public function __call($method, $parameters)
21+
{
22+
if ($method === 'withPrefix') {
23+
$this->prefix = $parameters[0];
24+
25+
return $this;
26+
}
27+
1528
if (!method_exists(Blueprint::class, $method)) {
1629
throw new RuntimeException('Undefined method [' . $method . '] called on Schema class.');
1730
}
@@ -21,29 +34,29 @@ public static function __callStatic($method, $parameters)
2134
}
2235

2336
if (\count($parameters) > 1 && $parameters[1] instanceof Closure) {
24-
$blueprint = self::createBlueprint($parameters[0], $method, $parameters[1]);
37+
$blueprint = $this->createBlueprint($parameters[0], $method, $parameters[1]);
2538
unset($parameters[0], $parameters[1]);
2639
} else {
27-
$blueprint = self::createBlueprint($parameters[0], $method);
40+
$blueprint = $this->createBlueprint($parameters[0], $method);
2841
unset($parameters[0]);
2942
}
3043

3144
\call_user_func_array([$blueprint, $method], $parameters);
3245

33-
return self::build($blueprint);
46+
return $this->build($blueprint);
3447
}
3548

36-
public static function createBlueprint($schema, $method, Closure $callback = null)
49+
public function createBlueprint($schema, $method, Closure $callback = null)
3750
{
3851
return new Blueprint(
3952
$schema,
4053
$method,
41-
Connection::getPrefix(),
54+
$this->prefix === '' ? Connection::getPrefix() : $this->prefix,
4255
$callback
4356
);
4457
}
4558

46-
public static function build(Blueprint $blueprint)
59+
public function build(Blueprint $blueprint)
4760
{
4861
return $blueprint->build();
4962
}

0 commit comments

Comments
 (0)