From 48c95c5e783e0170a20bab2828033896c07846c0 Mon Sep 17 00:00:00 2001 From: Tim Williams Date: Sun, 17 Jun 2018 21:32:52 +0100 Subject: [PATCH] Fix inserted _id on insert of EmbedsMany When an object is associated to parent in `associateNew`, a Mongo ID is created and added to the model regardless of the key set on the model. This commit changes the behaviour to only add the _id in instances where the primary key is set to be `_id`. This _may_ be a breaking change if code is depending on this behaviour; but the existing behaviour is undocumented. --- src/Jenssegers/Mongodb/Relations/EmbedsMany.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Jenssegers/Mongodb/Relations/EmbedsMany.php b/src/Jenssegers/Mongodb/Relations/EmbedsMany.php index b0e40893d..96ff1eab6 100644 --- a/src/Jenssegers/Mongodb/Relations/EmbedsMany.php +++ b/src/Jenssegers/Mongodb/Relations/EmbedsMany.php @@ -235,8 +235,8 @@ public function attach(Model $model) */ protected function associateNew($model) { - // Create a new key if needed. - if (!$model->getAttribute('_id')) { + // Create _id if needed. + if (!$model->getKey() && $model->getKeyName() === '_id') { $model->setAttribute('_id', new ObjectID); }