Skip to content

Commit 8d479bc

Browse files
feat(database): add a findBy method to models (#965)
Co-authored-by: Brent Roose <[email protected]>
1 parent 0688c97 commit 8d479bc

File tree

8 files changed

+56
-22
lines changed

8 files changed

+56
-22
lines changed

src/Tempest/Auth/src/SessionAuthenticator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ public function currentUser(): ?CanAuthenticate
4141
/** @var \Tempest\Database\Builder\ModelQueryBuilder<\Tempest\Auth\CanAuthenticate> $query */
4242
$query = $userModelClass->callStatic('query');
4343

44-
return $query->with('userPermissions.permission')->find($id);
44+
return $query->with('userPermissions.permission')->get($id);
4545
}
4646
}

src/Tempest/Database/src/Builder/ModelQueryBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function first(mixed ...$bindings)
5959
/**
6060
* @return TModelClass|null
6161
*/
62-
public function find(Id $id)
62+
public function get(Id $id)
6363
{
6464
return $this
6565
->whereField('id', $id)

src/Tempest/Database/src/DatabaseModel.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ public static function create(mixed ...$params): self;
2323

2424
public static function updateOrCreate(array $find, array $update): self;
2525

26-
public static function find(Id $id, array $relations = []): ?self;
26+
public static function get(Id $id, array $relations = []): ?self;
27+
28+
public static function find(mixed ...$conditions): ModelQueryBuilder;
2729

2830
public function save(): self;
2931

src/Tempest/Database/src/IsDatabaseModel.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,25 @@ public static function all(array $relations = []): array
8484
->all();
8585
}
8686

87-
public static function find(Id $id, array $relations = []): ?self
87+
public static function get(Id $id, array $relations = []): ?self
8888
{
8989
return self::query()
9090
->with(...$relations)
91-
->find($id);
91+
->get($id);
92+
}
93+
94+
public static function find(mixed ...$conditions): ModelQueryBuilder
95+
{
96+
$query = self::query();
97+
98+
array_walk($conditions, fn($value, $column) => $query->whereField($column, $value));
99+
100+
return $query;
92101
}
93102

94103
public function load(string ...$relations): self
95104
{
96-
$new = self::find($this->getId(), $relations);
105+
$new = self::get($this->getId(), $relations);
97106

98107
foreach (new ClassReflector($new)->getPublicProperties() as $property) {
99108
$property->setValue($this, $property->getValue($new));

src/Tempest/Router/src/RouteModelBindingInitializer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function initialize(ClassReflector $class, Container $container): object
3232
}
3333
}
3434

35-
$model = $class->callStatic('find', new Id($matchedRoute->params[$parameter->getName()]));
35+
$model = $class->callStatic('get', new Id($matchedRoute->params[$parameter->getName()]));
3636

3737
if ($model === null) {
3838
throw new NotFoundException();

tests/Integration/Database/QueryStatements/AlterTableStatementTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function test_it_can_alter_a_table_definition(): void
3737
$this->assertCount(2, MigrationModel::all());
3838
$this->assertSame(
3939
'0000-01-01_create_users_table',
40-
MigrationModel::find(new Id(2))->name,
40+
MigrationModel::get(new Id(2))->name,
4141
);
4242

4343
try {
@@ -61,7 +61,7 @@ public function test_it_can_alter_a_table_definition(): void
6161

6262
$this->assertSame(
6363
'0000-01-02_add_email_to_user_table',
64-
MigrationModel::find(new Id(3))->name,
64+
MigrationModel::get(new Id(3))->name,
6565
);
6666

6767
/** @var User $user */

tests/Integration/ORM/IsDatabaseModelTest.php

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function test_create_and_update_model(): void
5858
$this->assertSame('baz', $foo->bar);
5959
$this->assertInstanceOf(Id::class, $foo->id);
6060

61-
$foo = Foo::find($foo->id);
61+
$foo = Foo::get($foo->id);
6262

6363
$this->assertSame('baz', $foo->bar);
6464
$this->assertInstanceOf(Id::class, $foo->id);
@@ -67,7 +67,7 @@ public function test_create_and_update_model(): void
6767
bar: 'boo',
6868
);
6969

70-
$foo = Foo::find($foo->id);
70+
$foo = Foo::get($foo->id);
7171

7272
$this->assertSame('boo', $foo->bar);
7373
}
@@ -109,7 +109,7 @@ public function test_complex_query(): void
109109

110110
$book = $book->save();
111111

112-
$book = Book::find($book->id, relations: ['author']);
112+
$book = Book::get($book->id, relations: ['author']);
113113

114114
$this->assertEquals(1, $book->id->id);
115115
$this->assertSame('Book Title', $book->title);
@@ -273,7 +273,7 @@ public function test_has_many_through_relation(): void
273273
(new ThroughModel(parent: $parent, child: $childA))->save();
274274
(new ThroughModel(parent: $parent, child: $childB))->save();
275275

276-
$parent = ParentModel::find($parent->id, ['through.child']);
276+
$parent = ParentModel::get($parent->id, ['through.child']);
277277

278278
$this->assertSame('A', $parent->through[1]->child->name);
279279
$this->assertSame('B', $parent->through[2]->child->name);
@@ -290,7 +290,7 @@ public function test_empty_has_many_relation(): void
290290

291291
$parent = (new ParentModel(name: 'parent'))->save();
292292

293-
$parent = ParentModel::find($parent->id, ['through.child']);
293+
$parent = ParentModel::get($parent->id, ['through.child']);
294294

295295
$this->assertInstanceOf(ParentModel::class, $parent);
296296
$this->assertEmpty($parent->through);
@@ -313,8 +313,8 @@ public function test_has_one_relation(): void
313313

314314
(new ThroughModel(parent: $parent, child: $childA, child2: $childB))->save();
315315

316-
$child = ChildModel::find($childA->id, ['through.parent']);
317-
$child2 = ChildModel::find($childB->id, ['through2.parent']);
316+
$child = ChildModel::get($childA->id, ['through.parent']);
317+
$child2 = ChildModel::get($childB->id, ['through2.parent']);
318318

319319
$this->assertSame('parent', $child->through->parent->name);
320320
$this->assertSame('parent', $child2->through2->parent->name);
@@ -337,8 +337,8 @@ public function test_invalid_has_one_relation(): void
337337

338338
(new ThroughModel(parent: $parent, child: $childA, child2: $childB))->save();
339339

340-
$child = ChildModel::find($childA->id, ['through.parent']);
341-
$child2 = ChildModel::find($childB->id, ['through2.parent']);
340+
$child = ChildModel::get($childA->id, ['through.parent']);
341+
$child2 = ChildModel::get($childB->id, ['through2.parent']);
342342

343343
$this->assertSame('parent', $child->through->parent->name);
344344
$this->assertSame('parent', $child2->through2->parent->name);
@@ -462,8 +462,8 @@ public function test_delete(): void
462462

463463
$foo->delete();
464464

465-
$this->assertNull(Foo::find($foo->getId()));
466-
$this->assertNotNull(Foo::find($bar->getId()));
465+
$this->assertNull(Foo::get($foo->getId()));
466+
$this->assertNotNull(Foo::get($bar->getId()));
467467
}
468468

469469
public function test_property_with_carbon_type(): void
@@ -499,4 +499,27 @@ enum: CasterEnum::BAR,
499499
$this->assertSame(['a', 'b', 'c'], $model->array);
500500
$this->assertSame(CasterEnum::BAR, $model->enum);
501501
}
502+
503+
public function test_find(): void
504+
{
505+
$this->migrate(
506+
CreateMigrationsTable::class,
507+
CreateATable::class,
508+
CreateBTable::class,
509+
CreateCTable::class,
510+
);
511+
512+
(new C(name: 'one'))->save();
513+
(new C(name: 'two'))->save();
514+
515+
/** @var C[] */
516+
$valid = C::find(name: 'one')->all();
517+
518+
$this->assertCount(1, $valid);
519+
$this->assertSame($valid[0]->name, 'one');
520+
521+
$invalid = C::find(name: 'three')->all();
522+
523+
$this->assertCount(0, $invalid);
524+
}
502525
}

tests/Integration/Route/RequestTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public function test_custom_request_test_with_validation(): void
125125
)
126126
->assertStatus(Status::FOUND);
127127

128-
$book = Book::find(new Id(1));
128+
$book = Book::get(new Id(1));
129129
$this->assertSame(1, $book->id->id);
130130
$this->assertSame('a', $book->title);
131131
}
@@ -148,7 +148,7 @@ public function test_custom_request_test_with_nested_validation(): void
148148
)
149149
->assertStatus(Status::FOUND);
150150

151-
$book = Book::find(new Id(1), relations: ['author']);
151+
$book = Book::get(new Id(1), relations: ['author']);
152152
$this->assertSame(1, $book->id->id);
153153
$this->assertSame('a', $book->title);
154154
$this->assertSame('b', $book->author->name);

0 commit comments

Comments
 (0)