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

[Bug fix] BelongToMany relation with ObjectId #3015

Open
florianJacques opened this issue Jun 28, 2024 · 0 comments
Open

[Bug fix] BelongToMany relation with ObjectId #3015

florianJacques opened this issue Jun 28, 2024 · 0 comments

Comments

@florianJacques
Copy link
Contributor

florianJacques commented Jun 28, 2024

  • Laravel-mongodb Version: 4.5
  • PHP Version: 8.3
  • Database Driver & Version: MongoDB extension version => 1.18.0

Description:

When we use a BelongsToMany relationship with objectIf in foreignKey, this one are incorrectly cast.

Create Planet model:

<?php

declare(strict_types=1);

namespace App\Models\Test;

use MongoDB\Laravel\Eloquent\Model;
use MongoDB\Laravel\Relations\BelongsToMany;

class Planet extends Model
{
    protected $connection = 'mwe_mongodb';
    protected $collection = 'TestPlanet';

    public function getIdAttribute($value = null)
    {
        return $value;
    }

    public function visitors(): BelongsToMany
    {
        return $this->belongsToMany(SpaceExplorer::class, null, 'planetId', 'visitorId');
    }
}

Create SpaceExplorerModel

<?php

declare(strict_types=1);

namespace App\Models\Test;

use MongoDB\Laravel\Eloquent\Model;
use MongoDB\Laravel\Relations\BelongsToMany;

class SpaceExplorer extends Model
{
    protected $connection = 'mwe_mongodb';
    protected $collection = 'TestSpaceExplorer';

    public function getIdAttribute($value = null)
    {
        return $value;
    }

    public function planetsVisited(): BelongsToMany
    {
        return $this->belongsToMany(Planet::class, null, 'visitorIds', 'planetIds');
    }
}

And attach planet to spaceExplorer:

<?php

use App\Models\Test\Planet;
use App\Models\Test\SpaceExplorer;

$planetEarth = new Planet();
$planetEarth->name = 'Earth';
$planetEarth->save();
$planetJupiter = new Planet();
$planetJupiter->name = 'Jupiter';
$planetJupiter->save();
$explorerTanya = new SpaceExplorer();
$explorerTanya->name = 'Tanya Kirbuk';
$explorerTanya->save();
$explorerTanya->planetsVisited()->attach($planetEarth);
$explorerTanya->planetsVisited()->attach($planetJupiter);

Expected behaviour

The expected result

{
    "_id" : ObjectId("667e95f3759cbea7c80d1a24"),
    "name" : "Tanya Kirbuk",
    "updated_at" : ISODate("2024-06-28T10:52:35.735+0000"),
    "created_at" : ISODate("2024-06-28T10:52:35.735+0000"),
    "planetIds" : [
        ObjectId("667e95f3759cbea7c80d1a22"),
        ObjectId("667e95f3759cbea7c80d1a23")
    ]
}

Actual behaviour

The current result

{
    "_id" : ObjectId("667e967908ccc87ad000a294"),
    "name" : "Tanya Kirbuk",
    "updated_at" : ISODate("2024-06-28T10:54:49.690+0000"),
    "created_at" : ISODate("2024-06-28T10:54:49.690+0000"),
    "planetIds" : [
        {
            "oid" : "667e967908ccc87ad000a292"
        },
        {
            "oid" : "667e967908ccc87ad000a293"
        }
    ]
}
 

I created a Pull request to resolve the issue #3014

@florianJacques florianJacques changed the title [Bug fix] [Bug fix] BelongToMany relation with ObjectId Jun 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant