Skip to content

Commit c3d3d5f

Browse files
committed
schemaless instances with keys
1 parent 25de5ea commit c3d3d5f

2 files changed

Lines changed: 7 additions & 3 deletions

File tree

src/Database.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function cache(string $method, array $arguments, callable $callback)
6666

6767
public function create(string $class, array $data): object
6868
{
69-
if (property_exists($class, 'id') && (!array_key_exists('id', $data) || !$data['id'])) {
69+
if ((!class_exists($class, false) || property_exists($class, 'id')) && (!array_key_exists('id', $data) || !$data['id'])) {
7070
$data['id'] = $this->generateId($class);
7171
}
7272

@@ -141,7 +141,7 @@ public function findOrCreate(string $class, array $query, array $data = []): obj
141141

142142
$data = array_merge($query, $data);
143143

144-
if (property_exists($class, 'id') && (!array_key_exists('id', $data) || !$data['id'])) {
144+
if ((!class_exists($class, false) || property_exists($class, 'id')) && (!array_key_exists('id', $data) || !$data['id'])) {
145145
$data = array_merge($data, ['id' => $this->generateId($class)]);
146146
}
147147

@@ -166,6 +166,9 @@ public function findOrFail(string $class, array $query): ?object
166166
public function generateId(string $class): int|string
167167
{
168168
$model = $this->schema->getClassModel($class);
169+
if (!class_exists($class, false) && !$model) {
170+
return Sequence::getNext($this, $class);
171+
}
169172

170173
return match ($model->getProperties()[0]->type) {
171174
'int' => Sequence::getNext($this, $model->table),

tests/LegacyTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,9 @@ public function testRuntimeFreeDataSelect()
8989
$database->create('guard.access', ['hash' => 1]);
9090

9191
$this->assertCount(1, $database->find('guard.access'));
92-
$database->findOrCreate('guard.access', ['hash' => 2]);
92+
$this->assertObjectHasProperty('id', $database->findOne('guard.access', []));
9393

94+
$database->findOrCreate('guard.access', ['hash' => 2]);
9495
$this->assertCount(2, $database->find('guard.access'));
9596

9697
$database->findOrCreate('guard_access', ['hash' => 2]);

0 commit comments

Comments
 (0)