Skip to content

Commit

Permalink
Revert "[8.x] Fix HasOneOfMany with callback issue (#39187)"
Browse files Browse the repository at this point in the history
This reverts commit b8a0877.
  • Loading branch information
driesvints authored Oct 21, 2021
1 parent 41ac7e8 commit 3f8f9e1
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,7 @@ public function ofMany($column = 'id', $aggregate = 'MAX', $relation = null)

if (isset($previous)) {
$this->addOneOfManyJoinSubQuery($subQuery, $previous['subQuery'], $previous['column']);
}

if (isset($closure)) {
} elseif (isset($closure)) {
$closure($subQuery);
}

Expand Down Expand Up @@ -188,7 +186,7 @@ protected function newOneOfManySubQuery($groupBy, $column = null, $aggregate = n
}

if (! is_null($column)) {
$subQuery->selectRaw($aggregate.'('.$subQuery->getQuery()->grammar->wrap($column).') as '.$subQuery->getQuery()->grammar->wrap($column.'_aggregate'));
$subQuery->selectRaw($aggregate.'('.$subQuery->getQuery()->grammar->wrap($column).') as '.$subQuery->getQuery()->grammar->wrap($column));
}

$this->addOneOfManySubQueryConstraints($subQuery, $groupBy, $column, $aggregate);
Expand All @@ -210,7 +208,7 @@ protected function addOneOfManyJoinSubQuery(Builder $parent, Builder $subQuery,
$subQuery->applyBeforeQueryCallbacks();

$parent->joinSub($subQuery, $this->relationName, function ($join) use ($on) {
$join->on($this->qualifySubSelectColumn($on.'_aggregate'), '=', $this->qualifyRelatedColumn($on));
$join->on($this->qualifySubSelectColumn($on), '=', $this->qualifyRelatedColumn($on));

$this->addOneOfManyJoinSubQueryConstraints($join, $on);
});
Expand Down
40 changes: 3 additions & 37 deletions tests/Database/DatabaseEloquentHasOneOfManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public function createSchema()
$table->string('state');
$table->string('type');
$table->foreignId('user_id');
$table->timestamps();
});

$this->schema()->create('prices', function ($table) {
Expand Down Expand Up @@ -107,7 +106,7 @@ public function testEagerLoadingAppliesConstraintsToInnerJoinSubQuery()
$user = HasOneOfManyTestUser::create();
$relation = $user->latest_login();
$relation->addEagerConstraints([$user]);
$this->assertSame('select MAX("id") as "id_aggregate", "logins"."user_id" from "logins" where "logins"."user_id" = ? and "logins"."user_id" is not null and "logins"."user_id" in (1) group by "logins"."user_id"', $relation->getOneOfManySubQuery()->toSql());
$this->assertSame('select MAX("id") as "id", "logins"."user_id" from "logins" where "logins"."user_id" = ? and "logins"."user_id" is not null and "logins"."user_id" in (1) group by "logins"."user_id"', $relation->getOneOfManySubQuery()->toSql());
}

public function testQualifyingSubSelectColumn()
Expand Down Expand Up @@ -390,29 +389,6 @@ public function testWithSoftDeletes()
$this->assertNotNull($user->latest_login_with_soft_deletes);
}

public function testWithContraintNotInAggregate()
{
$user = HasOneOfManyTestUser::create();

$previousFoo = $user->states()->create([
'type' => 'foo',
'state' => 'bar',
'updated_at' => '2020-01-01 00:00:00',
]);
$newFoo = $user->states()->create([
'type' => 'foo',
'state' => 'active',
'updated_at' => '2021-01-01 12:00:00',
]);
$newBar = $user->states()->create([
'type' => 'bar',
'state' => 'active',
'updated_at' => '2021-01-01 12:00:00',
]);

$this->assertSame($newFoo->id, $user->last_updated_foo_state->id);
}

/**
* Get a database connection instance.
*
Expand Down Expand Up @@ -488,16 +464,6 @@ function ($q) {
);
}

public function last_updated_foo_state()
{
return $this->hasOne(HasOneOfManyTestState::class, 'user_id')->ofMany([
'updated_at' => 'max',
'id' => 'max',
], function ($q) {
$q->where('type', 'foo');
});
}

public function prices()
{
return $this->hasMany(HasOneOfManyTestPrice::class, 'user_id');
Expand Down Expand Up @@ -552,8 +518,8 @@ class HasOneOfManyTestState extends Eloquent
{
protected $table = 'states';
protected $guarded = [];
public $timestamps = true;
protected $fillable = ['type', 'state', 'updated_at'];
public $timestamps = false;
protected $fillable = ['type', 'state'];
}

class HasOneOfManyTestPrice extends Eloquent
Expand Down
2 changes: 1 addition & 1 deletion tests/Database/DatabaseEloquentMorphOneOfManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function testEagerLoadingAppliesConstraintsToInnerJoinSubQuery()
$product = MorphOneOfManyTestProduct::create();
$relation = $product->current_state();
$relation->addEagerConstraints([$product]);
$this->assertSame('select MAX("id") as "id_aggregate", "states"."stateful_id", "states"."stateful_type" from "states" where "states"."stateful_id" = ? and "states"."stateful_id" is not null and "states"."stateful_type" = ? and "states"."stateful_id" in (1) and "states"."stateful_type" = ? group by "states"."stateful_id", "states"."stateful_type"', $relation->getOneOfManySubQuery()->toSql());
$this->assertSame('select MAX("id") as "id", "states"."stateful_id", "states"."stateful_type" from "states" where "states"."stateful_id" = ? and "states"."stateful_id" is not null and "states"."stateful_type" = ? and "states"."stateful_id" in (1) and "states"."stateful_type" = ? group by "states"."stateful_id", "states"."stateful_type"', $relation->getOneOfManySubQuery()->toSql());
}

public function testReceivingModel()
Expand Down

0 comments on commit 3f8f9e1

Please sign in to comment.