laravel-mongodb
laravel-mongodb copied to clipboard
Trying to save an embedsMany relation results in the wrong query, fails.
- 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
- 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);
}
}
- 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'];
}
- 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}
)
Did you ever figure this out? Running into a similar issue.
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.
Did you ever figure this out? Running into a similar issue.
It works when you use create or update but not with save.