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

PHPORM-175: Use foreign key name for MorphTo relationships #3011

Merged
merged 4 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/Eloquent/HybridRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public function morphTo($name = null, $type = null, $id = null, $ownerKey = null
$this->newQuery(),
$this,
$id,
$ownerKey ?: $this->getKeyName(),
$ownerKey,
$type,
$name,
);
Expand Down
2 changes: 1 addition & 1 deletion src/Relations/MorphTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function addConstraints()
// or has many relationships, we need to actually query on the primary key
// of the related models matching on the foreign key that's on a parent.
$this->query->where(
$this->ownerKey,
$this->ownerKey ?? $this->getForeignKeyName(),
'=',
$this->getForeignKeyFrom($this->parent),
);
Expand Down
74 changes: 74 additions & 0 deletions tests/Ticket/GH2783Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

namespace MongoDB\Laravel\Tests\Ticket;

use MongoDB\Laravel\Eloquent\Model;
use MongoDB\Laravel\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\MorphOne;
jmikola marked this conversation as resolved.
Show resolved Hide resolved

/**
* @see https://github.com/mongodb/laravel-mongodb/issues/2783
* @see https://jira.mongodb.org/browse/PHPORM-175
*/
class GH2783Test extends TestCase
{
public function testMorphToInfersCustomOwnerKey()
{
GH2783Image::truncate();
GH2783Post::truncate();
GH2783User::truncate();

$post = GH2783Post::create(['text' => 'Lorem ipsum']);
$user = GH2783User::create(['username' => 'jsmith']);

$imageWithPost = GH2783Image::create(['uri' => 'http://example.com/post.png']);
$imageWithPost->imageable()->associate($post)->save();

$imageWithUser = GH2783Image::create(['uri' => 'http://example.com/user.png']);
$imageWithUser->imageable()->associate($user)->save();

$queriedImageWithPost = GH2783Image::with('imageable')->find($imageWithPost->getKey());
$this->assertInstanceOf(GH2783Post::class, $queriedImageWithPost->imageable);
$this->assertEquals($post->_id, $queriedImageWithPost->imageable->getKey());

$queriedImageWithUser = GH2783Image::with('imageable')->find($imageWithUser->getKey());
$this->assertInstanceOf(GH2783User::class, $queriedImageWithUser->imageable);
$this->assertEquals($user->username, $queriedImageWithUser->imageable->getKey());
}
}

class GH2783Image extends Model

Check failure on line 42 in tests/Ticket/GH2783Test.php

View workflow job for this annotation

GitHub Actions / phpcs

Each class must be in a file by itself
jmikola marked this conversation as resolved.
Show resolved Hide resolved
{
protected $connection = 'mongodb';
protected $fillable = ['uri'];

public function imageable(): MorphTo
{
return $this->morphTo(__FUNCTION__, 'imageable_type', 'imageable_id');
}
}

class GH2783Post extends Model

Check failure on line 53 in tests/Ticket/GH2783Test.php

View workflow job for this annotation

GitHub Actions / phpcs

Each class must be in a file by itself
{
protected $connection = 'mongodb';
protected $fillable = ['text'];

public function image(): MorphOne
{
return $this->morphOne(GH2783Image::class, 'imageable');
}
}

class GH2783User extends Model

Check failure on line 64 in tests/Ticket/GH2783Test.php

View workflow job for this annotation

GitHub Actions / phpcs

Each class must be in a file by itself
{
protected $connection = 'mongodb';
protected $fillable = ['username'];
protected $primaryKey = 'username';

public function image(): MorphOne
{
return $this->morphOne(GH2783Image::class, 'imageable');
}
}
Loading