-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PHPORM-175: Use foreign key name for MorphTo relationships
Incorporates the proposed solution in #2783 to not default $ownerKey to the current model's key name when constructing a MorphTo in HybridRelations::morphTo(). That change alone caused RelationsTest::testMorph() to fail, since MorphTo::addConstraints() would attempt to use a null ownerKey value. This required an additional change to fall back to the foreign key name when building the constraint.
- Loading branch information
Showing
3 changed files
with
76 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
/** | ||
* @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 | ||
{ | ||
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'); | ||
} | ||
} |