Skip to content

Commit 7cfa46d

Browse files
committed
Merge branch 'main' into database-null-issue
2 parents 292df08 + be9e80c commit 7cfa46d

File tree

7 files changed

+78
-35
lines changed

7 files changed

+78
-35
lines changed

.gitattributes

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
.gitignore merge=ours
2-
* text eol=lf
2+
* text eol=lf
3+
4+
/.vscode export-ignore
5+
lefthook.yml export-ignore
6+
.gitattributes export-ignore
7+
.gitignore export-ignore

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
".gitignore",
2525
"lefthook.yml",
2626
".php-cs-fixer.php",
27-
"composer.lock"
27+
"composer.lock",
28+
"/.vscode"
2829
]
2930
},
3031
"require": {

src/Blueprint.php

Lines changed: 10 additions & 2 deletions
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

@@ -745,7 +753,7 @@ private function addUniqueIndexQuery()
745753

746754
$query = '';
747755
foreach ($this->uniqueIndex as $key => $uniqueColumn) {
748-
$query .= "\nUNIQUE INDEX {$uniqueColumn}_UNIQUE ({$uniqueColumn} ASC) VISIBLE,";
756+
$query .= "\nUNIQUE INDEX {$uniqueColumn}_UNIQUE ({$uniqueColumn} ASC),";
749757
}
750758

751759
return $query;

src/Model.php

Lines changed: 16 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
) {
@@ -264,6 +272,11 @@ public function getTable()
264272
return $this->table;
265273
}
266274

275+
public function getPrefix()
276+
{
277+
return $this->prefix === '' ? Connection::getPrefix() : $this->prefix;
278+
}
279+
267280
public function getTableWithoutPrefix()
268281
{
269282
return $this->_tableWithoutPrefix;

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];
@@ -1339,9 +1339,10 @@ function ($value) {
13391339

13401340
if (
13411341
!empty($ids)
1342-
&& ($allRows = $this->newQuery()
1343-
->where($this->_model->getPrimaryKey(), $ids)
1344-
->get()
1342+
&& (
1343+
$allRows = $this->newQuery()
1344+
->where($this->_model->getPrimaryKey(), $ids)
1345+
->get()
13451346
)
13461347
) {
13471348
return $allRows;

src/Relations.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ public function newBelongsToMany($model, $foreignKey = null, $localKey = null)
9898
return $model->newQuery();
9999
}
100100

101+
/**
102+
* Returns list of query of relations
103+
*
104+
* @return array<string, QueryBuilder>
105+
*/
101106
public function getRelations()
102107
{
103108
return $this->_relations;
@@ -146,21 +151,13 @@ private function retrieveRelateData(QueryBuilder $query)
146151
. $parentQuery->select($relationKey['localKey'])->prepare()
147152
. ') AS subquery )'
148153
);
149-
if ($relationQuery->getModel()->getRelateAs() == 'oneToOne') {
150-
$relatedModels = $relationQuery->first();
151-
} else {
152-
$relatedModels = $relationQuery->get();
153-
}
154+
155+
$relatedModels = $relationQuery->get();
154156

155157
if ($relatedModels) {
156-
if ($relatedModels instanceof Model) {
157-
$this->_relatedData[$relationName][$relatedModels->getAttribute($relationKey['foreignKey'])]
158-
= $relatedModels;
159-
} else {
160-
foreach ($relatedModels as $relatedModel) {
161-
$this->_relatedData[$relationName][$relatedModel->getAttribute($relationKey['foreignKey'])][]
162-
= $relatedModel;
163-
}
158+
foreach ($relatedModels as $relatedModel) {
159+
$this->_relatedData[$relationName][$relatedModel->getAttribute($relationKey['foreignKey'])][]
160+
= $relatedModel;
164161
}
165162
}
166163
}
@@ -178,6 +175,11 @@ private function setRelatedData(Model $model)
178175
$data = isset(
179176
$this->_relatedData[$relationName][$model->getAttribute($relationKey['localKey'])]
180177
) ? $this->_relatedData[$relationName][$model->getAttribute($relationKey['localKey'])] : null;
178+
179+
if ($relationQuery->getModel()->getRelateAs() === 'oneToOne' && is_countable($data) && \count($data)) {
180+
$data = $data[0];
181+
}
182+
181183
$model->setAttribute($relationName, $data);
182184
}
183185
}

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)