Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Console/stubs/model/cache.stub
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class {className} extends Migration
$table->addString('key_name', ['primary' => true, 'size' => 500]);
$table->addText('data');
$table->addDatetime('expire', ['nullable' => true]);
$table->addIndex('expire');
$table->addTimestamps();
});
}
Expand Down
2 changes: 2 additions & 0 deletions src/Console/stubs/model/notification.stub
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class {className} extends Migration
$table->addString('type');
$table->addString('concern_id');
$table->addString('concern_type');
$table->addIndex('concern_id');
$table->addIndex('concern_type');
$table->addText('data');
$table->addDatetime('read_at', ['nullable' => true]);
$table->addTimestamps();
Expand Down
2 changes: 2 additions & 0 deletions src/Console/stubs/model/queue.stub
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class {className} extends Migration
$table->addDatetime('available_at');
$table->addDatetime('reserved_at', ["nullable" => true, "default" => null]);
$table->addDatetime('created_at');
$table->addIndex('queue');
$table->addIndex('status');
});
}

Expand Down
1 change: 1 addition & 0 deletions src/Console/stubs/model/session.stub
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class {className} extends Migration
$table->addTimestamp('time');
$table->addText('data');
$table->addString('ip');
$table->addIndex('time');
});
}

Expand Down
14 changes: 7 additions & 7 deletions src/Database/Barry/Concerns/Relationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,6 @@ public function belongsTo(
return new BelongsTo($related_model, $this, $foreign_key, $local_key);
}

/**
* Get the table key
*
* @return string
*/
abstract public function getKey(): string;

/**
* The belongs to many relative
*
Expand Down Expand Up @@ -128,4 +121,11 @@ public function hasOne(

return new HasOne($related_model, $this, $foreign_key, $primary_key);
}

/**
* Get the table key
*
* @return string
*/
abstract public function getKey(): string;
}
125 changes: 76 additions & 49 deletions src/Database/Barry/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,43 +16,57 @@ abstract class Relation
* @var bool
*/
protected static bool $has_constraints = true;

/**
* Indicate whether the relationships use a pivot table.*.
*
* @var bool
*/
protected static bool $has_pivot = false;

/**
* The foreign key of the parent model.
*
* @var string
*/
protected string $foreign_key;

/**
* The associated key on the parent model.
*
* @var string
*/
protected string $local_key;

/**
* The parent model instance
*
* @var Model
*/
protected Model $parent;

/**
* The related model instance
*
* @var Model
*/
protected Model $related;

/**
* The Bow Query builder
*
* @var QueryBuilder
*/
protected QueryBuilder $query;

/**
* Whether no parent exposed a key during eager loading, in which case the
* relation resolves to nothing without querying the database.
*
* @var bool
*/
protected bool $eager_has_no_keys = false;

/**
* Relation Contractor
*
Expand Down Expand Up @@ -84,6 +98,34 @@ public function __construct(Model $related, Model $parent)
*/
abstract public function addConstraints(): void;

/**
* Get the results of the relationship.
*
* @return mixed
*/
abstract public function getResults(): mixed;

/**
* The parent attribute whose value is matched against the related models.
*
* @return string
*/
abstract protected function eagerParentKey(): string;

/**
* The related column queried when eager loading the relation.
*
* @return string
*/
abstract protected function eagerRelatedKey(): string;

/**
* Whether the relation resolves to many related models.
*
* @return bool
*/
abstract protected function eagerIsMany(): bool;

/**
* Get the parent model.
*
Expand All @@ -104,24 +146,6 @@ public function getRelated(): Model
return $this->related;
}

/**
* _Call
*
* @param string $method
* @param array $args
* @return mixed
*/
public function __call(string $method, array $args = [])
{
$result = call_user_func_array([$this->query, $method], (array)$args);

if ($result === $this->query) {
return $this;
}

return $result;
}

/**
* Create a new row of the related
*
Expand All @@ -135,34 +159,6 @@ public function create(array $attributes): Model
return $this->related->create($attributes);
}

/**
* Get the results of the relationship.
*
* @return mixed
*/
abstract public function getResults(): mixed;

/**
* The parent attribute whose value is matched against the related models.
*
* @return string
*/
abstract protected function eagerParentKey(): string;

/**
* The related column queried when eager loading the relation.
*
* @return string
*/
abstract protected function eagerRelatedKey(): string;

/**
* Whether the relation resolves to many related models.
*
* @return bool
*/
abstract protected function eagerIsMany(): bool;

/**
* Run the given callback with relation constraints disabled.
*
Expand Down Expand Up @@ -197,9 +193,17 @@ public function addEagerConstraints(array $parents): void
fn ($value) => !is_null($value)
)));

// Fall back to an impossible match when no parent exposes a key so the
// query stays well-formed and returns nothing.
$this->query->whereIn($this->eagerRelatedKey(), count($keys) > 0 ? $keys : [0]);
// With no keys to match, skip the value-based constraint entirely.
// Injecting a placeholder value (such as 0) is rejected by strongly
// typed columns — e.g. a PostgreSQL uuid primary key raises
// "invalid input syntax for type uuid: 0". getEager() short-circuits.
if (count($keys) === 0) {
$this->eager_has_no_keys = true;

return;
}

$this->query->whereIn($this->eagerRelatedKey(), $keys);
}

/**
Expand All @@ -209,6 +213,11 @@ public function addEagerConstraints(array $parents): void
*/
public function getEager(): Collection
{
// No parent exposed a key, so there is nothing to fetch.
if ($this->eager_has_no_keys) {
return new Collection([]);
}

$results = $this->query->get();

return $results instanceof Collection ? $results : new Collection([]);
Expand Down Expand Up @@ -240,4 +249,22 @@ public function match(array $parents, Collection $results, string $name): void
);
}
}

/**
* _Call
*
* @param string $method
* @param array $args
* @return mixed
*/
public function __call(string $method, array $args = [])
{
$result = call_user_func_array([$this->query, $method], (array)$args);

if ($result === $this->query) {
return $this;
}

return $result;
}
}
2 changes: 1 addition & 1 deletion src/Database/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1587,7 +1587,7 @@ private function execute(string $sql, array $bindings = [], bool $write = true):
$this->triggerQueryEvent($sql, $ended_at - $start_at, $bindings);
} catch (\Exception $e) {
throw new QueryBuilderException(
'Error executing query: ' . $e->getMessage() . ' | Query: ' . $this->last_query,
'message: ' . $e->getMessage() . '; query: ' . $this->last_query,
$this->last_query,
E_ERROR,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class FakeCacheMigration extends Migration
$table->addString('key_name', ['primary' => true, 'size' => 500]);
$table->addText('data');
$table->addDatetime('expire', ['nullable' => true]);
$table->addIndex('expire');
$table->addTimestamps();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class FakeNotificationTableMigration extends Migration
$table->addString('type');
$table->addString('concern_id');
$table->addString('concern_type');
$table->addIndex('concern_id');
$table->addIndex('concern_type');
$table->addText('data');
$table->addDatetime('read_at', ['nullable' => true]);
$table->addTimestamps();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class QueueTableMigration extends Migration
$table->addDatetime('available_at');
$table->addDatetime('reserved_at', ["nullable" => true, "default" => null]);
$table->addDatetime('created_at');
$table->addIndex('queue');
$table->addIndex('status');
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class FakeSessionMigration extends Migration
$table->addTimestamp('time');
$table->addText('data');
$table->addString('ip');
$table->addIndex('time');
});
}

Expand Down
Loading
Loading