Skip to content

Improve error handling on unsupported hybrid queries #3404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 5.x
Choose a base branch
from
Open
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
26 changes: 25 additions & 1 deletion src/Helpers/QueriesRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ protected function isAcrossConnections(Relation $relation)
*/
public function addHybridHas(Relation $relation, $operator = '>=', $count = 1, $boolean = 'and', ?Closure $callback = null)
{
$this->assertHybridRelationSupported($relation);

$hasQuery = $relation->getQuery();
if ($callback) {
$hasQuery->callScope($callback);
Expand All @@ -128,6 +130,26 @@ public function addHybridHas(Relation $relation, $operator = '>=', $count = 1, $
return $this->whereIn($this->getRelatedConstraintKey($relation), $relatedIds, $boolean, $not);
}

/**
* @param Relation $relation
*
* @return void
*
* @throws Exception
*/
public function assertHybridRelationSupported(Relation $relation): void
{
if (
$relation instanceof HasOneOrMany
|| $relation instanceof BelongsTo
|| ($relation instanceof BelongsToMany && ! $this->isAcrossConnections($relation))
) {
return;
}

throw new Exception(class_basename($relation) . ' is not supported for hybrid query constraints.');
}

/**
* @param Builder $hasQuery
* @param Relation $relation
Expand Down Expand Up @@ -213,6 +235,8 @@ protected function getConstrainedRelatedIds($relations, $operator, $count)
*/
protected function getRelatedConstraintKey(Relation $relation)
{
$this->assertHybridRelationSupported($relation);

if ($relation instanceof HasOneOrMany) {
return $relation->getLocalKeyName();
}
Expand All @@ -221,7 +245,7 @@ protected function getRelatedConstraintKey(Relation $relation)
return $relation->getForeignKeyName();
}

if ($relation instanceof BelongsToMany && ! $this->isAcrossConnections($relation)) {
if ($relation instanceof BelongsToMany) {
return $this->model->getKeyName();
}

Expand Down
31 changes: 24 additions & 7 deletions tests/HybridRelationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function testSqlRelations()
$this->assertEquals('John Doe', $role->sqlUser->name);

// MongoDB User
$user = new User();
$user = new User();
$user->name = 'John Doe';
$user->save();

Expand All @@ -105,7 +105,7 @@ public function testSqlRelations()

public function testHybridWhereHas()
{
$user = new SqlUser();
$user = new SqlUser();
$otherUser = new SqlUser();
$this->assertInstanceOf(SqlUser::class, $user);
$this->assertInstanceOf(SQLiteConnection::class, $user->getConnection());
Expand All @@ -114,11 +114,11 @@ public function testHybridWhereHas()

// SQL User
$user->name = 'John Doe';
$user->id = 2;
$user->id = 2;
$user->save();
// Other user
$otherUser->name = 'Other User';
$otherUser->id = 3;
$otherUser->id = 3;
$otherUser->save();
// Make sure they are created
$this->assertIsInt($user->id);
Expand Down Expand Up @@ -159,7 +159,7 @@ public function testHybridWhereHas()

public function testHybridWith()
{
$user = new SqlUser();
$user = new SqlUser();
$otherUser = new SqlUser();
$this->assertInstanceOf(SqlUser::class, $user);
$this->assertInstanceOf(SQLiteConnection::class, $user->getConnection());
Expand All @@ -168,11 +168,11 @@ public function testHybridWith()

// SQL User
$user->name = 'John Doe';
$user->id = 2;
$user->id = 2;
$user->save();
// Other user
$otherUser->name = 'Other User';
$otherUser->id = 3;
$otherUser->id = 3;
$otherUser->save();
// Make sure they are created
$this->assertIsInt($user->id);
Expand Down Expand Up @@ -268,6 +268,23 @@ public function testHybridBelongsToMany()
$this->assertEquals(1, $check->skills->count());
}

public function testQueryingHybridBelongsToManyRelationFails()
{
$user = new SqlUser();
$this->assertInstanceOf(SQLiteConnection::class, $user->getConnection());

// Create Mysql Users
$user->fill(['name' => 'John Doe'])->save();
$skill = Skill::query()->create(['name' => 'MongoDB']);
$user->skills()->save($skill);

$this->expectExceptionMessage('BelongsToMany is not supported for hybrid query constraints.');

SqlUser::whereHas('skills', function ($query) {
return $query->where('name', 'LIKE', 'MongoDB');
});
}

public function testHybridMorphToManySqlModelToMongoModel()
{
// SqlModel -> MorphToMany -> MongoModel
Expand Down
Loading