Skip to content
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

[8.x] Add default "_of_many" to join alias when relation name is table name #37411

Merged
merged 2 commits into from
May 19, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ public function ofMany($column = 'id', $aggregate = 'MAX', $relation = null)
{
$this->isOneOfMany = true;

$this->relationName = $relation ?: $this->guessRelationship();
$this->relationName = $relation ?: $this->getDefaultOneOfManyJoinAlias(
$this->guessRelationship()
);

$keyName = $this->query->getModel()->getKeyName();

Expand Down Expand Up @@ -110,6 +112,19 @@ public function ofMany($column = 'id', $aggregate = 'MAX', $relation = null)
return $this;
}

/**
* Get the default alias for one of many inner join clause.
*
* @param string $relation
* @return string
*/
protected function getDefaultOneOfManyJoinAlias($relation)
{
return $relation == $this->query->getModel()->getTable()
? $relation.'_of_many'
: $relation;
}

/**
* Indicate that the relation is the latest single result of a larger one-to-many relationship.
*
Expand Down
40 changes: 31 additions & 9 deletions tests/Database/DatabaseEloquentHasOneOfManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,32 @@ protected function tearDown(): void

public function testItGuessesRelationName()
{
$user = HasOneOfManyTestUser::create();
$user = HasOneOfManyTestUser::make();
$this->assertSame('latest_login', $user->latest_login()->getRelationName());
}

// public function testRelationNameCanBeSet()
// {
// $user = HasOneOfManyTestUser::create();
// $this->assertSame('foo', $user->latest_login_with_other_name()->getRelationName());
// }
public function testItGuessesRelationNameAndAddsOfManyWhenTableNameIsRelationName()
{
$model = HasOneOfManyTestModel::make();
$this->assertSame('logins_of_many', $model->logins()->getRelationName());
}

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

// Using "ofMany"
$relation = $user->latest_login()->ofMany('id', 'max', 'foo');
$this->assertSame('foo', $relation->getRelationName());

// Using "latestOfMAny"
$relation = $user->latest_login()->latestOfMAny('id', 'bar');
$this->assertSame('bar', $relation->getRelationName());

// Using "oldestOfMAny"
$relation = $user->latest_login()->oldestOfMAny('id', 'baz');
$this->assertSame('baz', $relation->getRelationName());
}

public function testQualifyingSubSelectColumn()
{
Expand Down Expand Up @@ -225,9 +242,6 @@ public function testIsNotMethod()
$this->assertFalse($user->latest_login()->isNot($login2));
}

/**
* @group fail
*/
public function testGet()
{
$user = HasOneOfManyTestUser::create();
Expand Down Expand Up @@ -392,6 +406,14 @@ public function price_with_shortcut()
}
}

class HasOneOfManyTestModel extends Eloquent
{
public function logins()
{
return $this->hasOne(HasOneOfManyTestLogin::class)->ofMany();
}
}

class HasOneOfManyTestLogin extends Eloquent
{
protected $table = 'logins';
Expand Down