Skip to content

Commit 9b71a17

Browse files
committed
wip
1 parent 1279dfd commit 9b71a17

19 files changed

+109
-64
lines changed

packages/database/src/Builder/ModelInspector.php

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,27 @@ final class ModelInspector
2727
{
2828
private ?ClassReflector $modelClass;
2929

30-
public function __construct(
31-
private object|string $model,
32-
)
30+
private object|string $model;
31+
32+
public function __construct(object|string $model)
3333
{
34-
if ($this->model instanceof ClassReflector) {
35-
$this->modelClass = $this->model;
34+
if ($model instanceof HasMany) {
35+
$model = $model->property->getIterableType()->asClass();
36+
$this->modelClass = $model;
37+
} elseif ($model instanceof BelongsTo || $model instanceof HasOne) {
38+
$model = $model->property->getType()->asClass();
39+
$this->modelClass = $model;
40+
} elseif ($model instanceof ClassReflector) {
41+
$this->modelClass = $model;
3642
} else {
3743
try {
38-
$this->modelClass = new ClassReflector($this->model);
44+
$this->modelClass = new ClassReflector($model);
3945
} catch (ReflectionException) {
4046
$this->modelClass = null;
4147
}
4248
}
49+
50+
$this->model = $model;
4351
}
4452

4553
public function isObjectModel(): bool
@@ -247,13 +255,12 @@ public function resolveRelations(string $relationString, string $parent = ''): a
247255
$currentRelationName,
248256
), '.');
249257

250-
return [
251-
$currentRelation,
252-
...model($currentRelation->property->getType()->asClass())->resolveRelations($newRelationString, $newParent)
253-
];
258+
$relations = [$currentRelation];
259+
260+
return [...$relations, ...model($currentRelation)->resolveRelations($newRelationString, $newParent)];
254261
}
255262

256-
public function getEagerRelations(): array
263+
public function resolveEagerRelations(string $parent = ''): array
257264
{
258265
if (! $this->isObjectModel()) {
259266
return [];
@@ -262,8 +269,26 @@ public function getEagerRelations(): array
262269
$relations = [];
263270

264271
foreach ($this->modelClass->getPublicProperties() as $property) {
265-
if ($property->hasAttribute(Eager::class)) {
266-
$relations[$property->getName()] = $this->getRelation($property);
272+
if (! $property->hasAttribute(Eager::class)) {
273+
continue;
274+
}
275+
276+
$currentRelationName = $property->getName();
277+
$currentRelation = $this->getRelation($currentRelationName);
278+
279+
if (! $currentRelation) {
280+
continue;
281+
}
282+
283+
$relations[$property->getName()] = $currentRelation->setParent($parent);
284+
$newParent = ltrim(sprintf(
285+
'%s.%s',
286+
$parent,
287+
$currentRelationName,
288+
), '.');
289+
290+
foreach (model($currentRelation)->resolveEagerRelations($newParent) as $name => $nestedEagerRelation) {
291+
$relations[$name] = $nestedEagerRelation;
267292
}
268293
}
269294

packages/database/src/Builder/QueryBuilders/CountQueryBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function bind(mixed ...$bindings): self
9090

9191
public function toSql(): string
9292
{
93-
return $this->build()->getSql();
93+
return $this->build()->toSql();
9494
}
9595

9696
public function build(mixed ...$bindings): Query

packages/database/src/Builder/QueryBuilders/DeleteQueryBuilder.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ public function bind(mixed ...$bindings): self
6161
return $this;
6262
}
6363

64+
public function toSql(): string
65+
{
66+
return $this->build()->toSql();
67+
}
68+
6469
public function build(mixed ...$bindings): Query
6570
{
6671
return new Query($this->delete, [...$this->bindings, ...$bindings]);

packages/database/src/Builder/QueryBuilders/InsertQueryBuilder.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ public function execute(mixed ...$bindings): Id
4545
return $id;
4646
}
4747

48+
public function toSql(): string
49+
{
50+
return $this->build()->toSql();
51+
}
52+
4853
public function build(mixed ...$bindings): Query
4954
{
5055
$definition = model($this->model);

packages/database/src/Builder/QueryBuilders/SelectQueryBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public function bind(mixed ...$bindings): self
213213

214214
public function toSql(): string
215215
{
216-
return $this->build()->getSql();
216+
return $this->build()->toSql();
217217
}
218218

219219
public function build(mixed ...$bindings): Query
@@ -256,7 +256,7 @@ private function getIncludedRelations(): array
256256
return [];
257257
}
258258

259-
$relations = $definition->getEagerRelations();
259+
$relations = $definition->resolveEagerRelations();
260260

261261
foreach ($this->relations as $relationString) {
262262
$resolvedRelations = $definition->resolveRelations($relationString);

packages/database/src/Builder/QueryBuilders/UpdateQueryBuilder.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ public function bind(mixed ...$bindings): self
6262
return $this;
6363
}
6464

65+
public function toSql(): string
66+
{
67+
return $this->build()->toSql();
68+
}
69+
6570
public function build(mixed ...$bindings): Query
6671
{
6772
$values = $this->resolveValues();

packages/database/src/Exceptions/QueryException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function __construct(Query $query, array $bindings, PDOException $previou
1414
{
1515
$message = $previous->getMessage();
1616

17-
$message .= PHP_EOL . PHP_EOL . $query->getSql() . PHP_EOL;
17+
$message .= PHP_EOL . PHP_EOL . $query->toSql() . PHP_EOL;
1818

1919
$message .= PHP_EOL . 'bindings: ' . json_encode($bindings, JSON_PRETTY_PRINT);
2020

packages/database/src/GenericDatabase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function execute(Query $query): void
2626

2727
try {
2828
$this->connection
29-
->prepare($query->getSql())
29+
->prepare($query->toSql())
3030
->execute($bindings);
3131
} catch (PDOException $pdoException) {
3232
throw new QueryException($query, $bindings, $pdoException);
@@ -40,7 +40,7 @@ public function getLastInsertId(): Id
4040

4141
public function fetch(Query $query): array
4242
{
43-
$pdoQuery = $this->connection->prepare($query->getSql());
43+
$pdoQuery = $this->connection->prepare($query->toSql());
4444

4545
$pdoQuery->execute($this->resolveBindings($query));
4646

packages/database/src/Mappers/SelectModelMapper.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,27 @@ private function normalizeFields(ModelInspector $model, array $rows): array
5454
// BelongsTo
5555
if ($belongsTo = $model->getBelongsTo($mainField)) {
5656
$data[$belongsTo->property->getName()][str_replace($mainField . '.', '', $field)] = $value;
57+
continue;
5758
}
5859

5960
// HasOne
6061
if ($hasOne = $model->getHasOne($mainField)) {
6162
$data[$hasOne->property->getName()][str_replace($mainField . '.', '', $field)] = $value;
63+
continue;
6264
}
6365

6466
// HasMany
6567
if ($hasMany = $model->getHasMany($mainField)) {
66-
$hasManyRelations[$mainField] ??= $hasMany;
67-
6868
$hasManyId = $row[$hasMany->idField()];
6969

70+
if ($hasManyId === null) {
71+
// Empty has many relations are initialized it with an empty array
72+
$data[$hasMany->property->getName()] ??= [];
73+
continue;
74+
}
75+
76+
$hasManyRelations[$mainField] ??= $hasMany;
77+
7078
$data[$hasMany->property->getName()][$hasManyId][str_replace($mainField . '.', '', $field)] = $value;
7179
}
7280
}

packages/database/src/Migrations/MigrationManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ private function getMinifiedSqlFromStatement(?QueryStatement $statement): string
281281
$query = new Query($statement->compile($this->databaseConfig->dialect));
282282

283283
// Remove comments
284-
$sql = preg_replace('/--.*$/m', '', $query->getSql()); // Remove SQL single-line comments
284+
$sql = preg_replace('/--.*$/m', '', $query->toSql()); // Remove SQL single-line comments
285285
$sql = preg_replace('/\/\*[\s\S]*?\*\//', '', $sql); // Remove block comments
286286

287287
// Remove blank lines and excessive spaces

0 commit comments

Comments
 (0)