Skip to content

Commit e802f6c

Browse files
committed
init fixes
1 parent 20c4ea8 commit e802f6c

File tree

5 files changed

+83
-29
lines changed

5 files changed

+83
-29
lines changed

src/Database/Connection.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@
1010

1111
class Connection extends \Tinderbox\ClickhouseBuilder\Integrations\Laravel\Connection
1212
{
13-
/**
14-
* @return Builder|\Tinderbox\ClickhouseBuilder\Integrations\Laravel\Builder
15-
*/
16-
public function query()
13+
public function query(): Builder
1714
{
1815
return new Builder($this, new Grammar());
1916
}

src/Database/Eloquent/Builder.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,13 @@ public function newModelInstance($attributes = [])
652652
->setConnection($this->query->getConnection()->getName());
653653
}
654654

655+
public function create ($attributes = [])
656+
{
657+
return tap($this->newModelInstance($attributes), static function ($instance) {
658+
$instance->save();
659+
});
660+
}
661+
655662
public function getQuery(): QueryBuilder
656663
{
657664
return $this->query;

src/Database/Eloquent/Concerns/HasAttributes.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@ trait HasAttributes
1212

1313
public function getDates(): array
1414
{
15-
return property_exists($this, 'dates') && is_array($this->dates)
16-
? $this->dates
17-
: [];
15+
return $this->dates ?? [];
1816
}
1917

2018
public function getCasts(): array
2119
{
22-
return $this->casts;
20+
return $this->casts ?? [];
2321
}
2422

2523
protected function getDateFormat(): string

src/Database/Eloquent/Model.php

Lines changed: 72 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
use ArrayAccess;
88
use Bavix\LaravelClickHouse\Database\Connection;
99
use Bavix\LaravelClickHouse\Database\Query\Builder as QueryBuilder;
10+
use Illuminate\Contracts\Events\Dispatcher;
11+
use Illuminate\Contracts\Routing\UrlRoutable;
1012
use Illuminate\Contracts\Support\Arrayable;
1113
use Illuminate\Contracts\Support\Jsonable;
14+
use Illuminate\Database\ConnectionInterface;
1215
use Illuminate\Database\ConnectionResolverInterface;
1316
use Illuminate\Database\ConnectionResolverInterface as Resolver;
1417
use Illuminate\Database\Eloquent\Concerns\GuardsAttributes;
@@ -17,11 +20,14 @@
1720
use Illuminate\Database\Eloquent\Concerns\HidesAttributes;
1821
use Illuminate\Database\Eloquent\JsonEncodingException;
1922
use Illuminate\Database\Eloquent\MassAssignmentException;
23+
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
24+
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
25+
use Illuminate\Database\Eloquent\Relations\Relation;
2026
use Illuminate\Support\Str;
2127
use JsonSerializable;
2228
use Tinderbox\ClickhouseBuilder\Query\Grammar;
2329

24-
abstract class Model implements ArrayAccess, Arrayable, Jsonable, JsonSerializable
30+
abstract class Model implements ArrayAccess, UrlRoutable, Arrayable, Jsonable, JsonSerializable
2531
{
2632
use Concerns\HasAttributes;
2733
use Concerns\Common;
@@ -35,7 +41,7 @@ abstract class Model implements ArrayAccess, Arrayable, Jsonable, JsonSerializab
3541
*
3642
* @var bool
3743
*/
38-
public $exists = false;
44+
public bool $exists = false;
3945

4046
/**
4147
* Indicates if an exception should be thrown when trying to access a missing attribute on a retrieved model.
@@ -93,19 +99,9 @@ abstract class Model implements ArrayAccess, Arrayable, Jsonable, JsonSerializab
9399
*/
94100
protected $perPage = 15;
95101

96-
/**
97-
* The connection resolver instance.
98-
*
99-
* @var Resolver
100-
*/
101-
protected static $resolver;
102+
protected static ConnectionResolverInterface $resolver;
102103

103-
/**
104-
* The event dispatcher instance.
105-
*
106-
* @var \Illuminate\Contracts\Events\Dispatcher
107-
*/
108-
protected static $dispatcher;
104+
protected static Dispatcher $dispatcher;
109105

110106
/**
111107
* The array of booted models.
@@ -114,6 +110,8 @@ abstract class Model implements ArrayAccess, Arrayable, Jsonable, JsonSerializab
114110
*/
115111
protected static $booted = [];
116112

113+
public bool $wasRecentlyCreated = false;
114+
117115
/**
118116
* Create a new Eloquent model instance.
119117
*/
@@ -381,7 +379,7 @@ public function jsonSerialize(): array
381379
/**
382380
* Get the database connection for the model.
383381
*/
384-
public function getConnection(): \Illuminate\Database\Connection
382+
public function getConnection(): ConnectionInterface
385383
{
386384
return static::resolveConnection($this->getConnectionName());
387385
}
@@ -408,10 +406,8 @@ public function setConnection(string $name)
408406

409407
/**
410408
* Resolve a connection instance.
411-
*
412-
* @return Connection|\Illuminate\Database\ConnectionInterface
413409
*/
414-
public static function resolveConnection(string $connection = null)
410+
public static function resolveConnection(string $connection = null): ConnectionInterface
415411
{
416412
return static::getConnectionResolver()->connection($connection);
417413
}
@@ -456,6 +452,52 @@ public function setTable(string $table): self
456452
return $this;
457453
}
458454

455+
public function resolveRouteBinding($value, $field = null): ?Model
456+
{
457+
return $this->resolveRouteBindingQuery($this, $value, $field)->first();
458+
}
459+
460+
public function resolveSoftDeletableRouteBinding(mixed $value, ?string $field = null): ?Model
461+
{
462+
return $this->resolveRouteBindingQuery($this, $value, $field)->withTrashed()->first();
463+
}
464+
465+
public function resolveChildRouteBinding($childType, $value, $field): ?Model
466+
{
467+
return $this->resolveChildRouteBindingQuery($childType, $value, $field)->first();
468+
}
469+
470+
public function resolveSoftDeletableChildRouteBinding(string $childType, mixed $value, ?string $field = null): ?Model
471+
{
472+
return $this->resolveChildRouteBindingQuery($childType, $value, $field)->withTrashed()->first();
473+
}
474+
475+
protected function resolveChildRouteBindingQuery(string $childType, mixed $value, ?string $field = null): Relation|Model
476+
{
477+
$relationship = $this->{$this->childRouteBindingRelationshipName($childType)}();
478+
479+
$field = $field ?: $relationship->getRelated()->getRouteKeyName();
480+
481+
if ($relationship instanceof HasManyThrough ||
482+
$relationship instanceof BelongsToMany) {
483+
$field = $relationship->getRelated()->getTable().'.'.$field;
484+
}
485+
486+
return $relationship instanceof Model
487+
? $relationship->resolveRouteBindingQuery($relationship, $value, $field)
488+
: $relationship->getRelated()->resolveRouteBindingQuery($relationship, $value, $field);
489+
}
490+
491+
protected function childRouteBindingRelationshipName(string $childType): string
492+
{
493+
return Str::plural(Str::camel($childType));
494+
}
495+
496+
public function resolveRouteBindingQuery(Relation|Model $query, mixed $value, ?string $field = null): Builder
497+
{
498+
return $query->where($field ?? $this->getRouteKeyName(), $value);
499+
}
500+
459501
/**
460502
* Get the primary key for the model.
461503
*/
@@ -504,6 +546,16 @@ public function setKeyType(string $type)
504546
return $this;
505547
}
506548

549+
public function getRouteKey(): mixed
550+
{
551+
return $this->getAttribute($this->getRouteKeyName());
552+
}
553+
554+
public function getRouteKeyName(): string
555+
{
556+
return $this->getKeyName();
557+
}
558+
507559
/**
508560
* Get the value of the model's primary key.
509561
*
@@ -533,9 +585,9 @@ public function getPerPage(): int
533585
/**
534586
* Set the number of models to return per page.
535587
*
536-
* @return $this
588+
* @return static
537589
*/
538-
public function setPerPage(int $perPage): self
590+
public function setPerPage(int $perPage): static
539591
{
540592
$this->perPage = $perPage;
541593

src/Database/Query/Builder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Builder extends BaseBuilder
1919
__call as macroCall;
2020
}
2121

22-
protected $connection;
22+
protected Connection $connection;
2323

2424
public function __construct(
2525
Connection $connection,

0 commit comments

Comments
 (0)