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 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
4 changes: 4 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,8 @@
<exclude-pattern>docs/**/*.php</exclude-pattern>
</exclude>
</rule>

<rule ref="PSR1.Classes.ClassDeclaration.MultipleClasses">
<exclude-pattern>tests/Ticket/*.php</exclude-pattern>
</rule>
</ruleset>
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
75 changes: 75 additions & 0 deletions tests/Ticket/GH2783Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

namespace MongoDB\Laravel\Tests\Ticket;

use Illuminate\Database\Eloquent\Relations\MorphOne;
use MongoDB\Laravel\Eloquent\Model;
use MongoDB\Laravel\Relations\MorphTo;
use MongoDB\Laravel\Tests\TestCase;

/**
* @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
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
{
protected $connection = 'mongodb';
protected $fillable = ['text'];

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

class GH2783User extends Model
{
protected $connection = 'mongodb';
protected $fillable = ['username'];
protected $primaryKey = 'username';

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