Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Trying to save an embedsMany relation results in the wrong query, fails. #2519

Open
trip-somers opened this issue Mar 2, 2023 · 3 comments
Labels
relations Relations between documents

Comments

@trip-somers
Copy link

trip-somers commented Mar 2, 2023

  • Laravel-mongodb Version: 3.8.5
  • PHP Version: 8.1.6
  • Database Driver & Version: 4.0.20 (I am not currently in a position to be able to upgrade, so I hope this isn't the core problem.)

Description:

Attempting to store an embedded model via the embedsMany relationship results in an incorrect query causing the store to fail.

Steps to reproduce

  1. Create a parent model and collection. In this case, mine is called 'Organization' and looks like this:
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Jenssegers\Mongodb\Eloquent\Model;

class Organization extends Model
{
    use HasFactory;

    protected $connection = 'mongodb';
    protected $collection = 'organizations';

    protected $guarded = ['id', '_id'];

    public function paymentMethods()
    {
        return $this->embedsMany(PaymentMethod::class);
    }
}
  1. Create the embedded model. In this case, mine is called 'PaymentMethod' and looks like this:
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Jenssegers\Mongodb\Eloquent\Model;

class PaymentMethod extends Model
{
    use HasFactory;

    protected $guarded = ['id', '_id'];
}
  1. Attempt to store a new PaymentMethod for an Organization:
$organization->paymentMethods()->create($params);

Expected behaviour

A PaymentMethod document is embedded within the Organization document.

Actual behaviour

The following operation is executed. It tries to find an Organization with a PaymentMethod ID that matches the Organization's _id, creating an impossible condition.

organizations.UpdateMany(
    {"paymentMethods._id":"636aa6bf4bbb1badfa05c0e2"},
    {"$set":{
        "paymentMethods.$.updated_at":{"$date":{"$numberLong":"1677771653893"}},
        "paymentMethods.$.paymentMethods":[{
            "customer_id":239304,
            "remote_id":236547,
            "account_type":"visa",
            "account_last_4":"1111",
            "expiration_month":12,
            "expiration_year":2025,
            "billing_name":"Test",
            "updated_at":{"$date":{"$numberLong":"1677771653891"}},
            "created_at":{"$date":{"$numberLong":"1677771653891"}},
            "_id":"6400c38534e05e1b000214ba"
        }]
    }},
    {"multiple":true}
)
@sampedraza
Copy link

Did you ever figure this out? Running into a similar issue.

@trip-somers
Copy link
Author

trip-somers commented Jun 12, 2023

Nope. We wound up using a MongoDB anti-pattern and putting the PaymentMethod model in its own table. I have avoided using this package when I can. I assume there's a fork out there somewhere that fixes this, but I haven't looked for it yet.

@mongodb mongodb deleted a comment from ChefDeli103 Nov 10, 2023
@GromNaN GromNaN added the relations Relations between documents label Nov 10, 2023
@omarherri
Copy link

Did you ever figure this out? Running into a similar issue.

It works when you use create or update but not with save.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
relations Relations between documents
Projects
None yet
Development

No branches or pull requests

4 participants