Skip to content

Commit

Permalink
[8.x] Add default "_of_many" to join alias when relation name is tabl…
Browse files Browse the repository at this point in the history
…e name (#37411)

* improved default alias

* Update CanBeOneOfMany.php

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
cbl and taylorotwell authored May 19, 2021
1 parent b481c3a commit 1996e69
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
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

0 comments on commit 1996e69

Please sign in to comment.