Commit 79ea62d0 authored by milanspv's avatar milanspv

do not use array dot notation for updating embedded models

parent 1bb259ae
...@@ -6,7 +6,6 @@ use Illuminate\Database\Eloquent\Collection; ...@@ -6,7 +6,6 @@ use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator; use Illuminate\Pagination\Paginator;
use Illuminate\Support\Arr;
use MongoDB\BSON\ObjectID; use MongoDB\BSON\ObjectID;
class EmbedsMany extends EmbedsOneOrMany class EmbedsMany extends EmbedsOneOrMany
...@@ -79,8 +78,7 @@ class EmbedsMany extends EmbedsOneOrMany ...@@ -79,8 +78,7 @@ class EmbedsMany extends EmbedsOneOrMany
// Get the correct foreign key value. // Get the correct foreign key value.
$foreignKey = $this->getForeignKeyValue($model); $foreignKey = $this->getForeignKeyValue($model);
// Use array dot notation for better update behavior. $values = $this->getUpdateValues($model->getDirty(), $this->localKey . '.$.');
$values = Arr::dot($model->getDirty(), $this->localKey . '.$.');
// Update document in database. // Update document in database.
$result = $this->getBaseQuery()->where($this->localKey . '.' . $model->getKeyName(), $foreignKey) $result = $this->getBaseQuery()->where($this->localKey . '.' . $model->getKeyName(), $foreignKey)
......
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
namespace Jenssegers\Mongodb\Relations; namespace Jenssegers\Mongodb\Relations;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use MongoDB\BSON\ObjectID; use MongoDB\BSON\ObjectID;
class EmbedsOne extends EmbedsOneOrMany class EmbedsOne extends EmbedsOneOrMany
...@@ -71,8 +70,7 @@ class EmbedsOne extends EmbedsOneOrMany ...@@ -71,8 +70,7 @@ class EmbedsOne extends EmbedsOneOrMany
return $this->parent->save(); return $this->parent->save();
} }
// Use array dot notation for better update behavior. $values = $this->getUpdateValues($model->getDirty(), $this->localKey . '.');
$values = Arr::dot($model->getDirty(), $this->localKey . '.');
$result = $this->getBaseQuery()->update($values); $result = $this->getBaseQuery()->update($values);
......
...@@ -375,4 +375,22 @@ abstract class EmbedsOneOrMany extends Relation ...@@ -375,4 +375,22 @@ abstract class EmbedsOneOrMany extends Relation
{ {
return $this->parent->getKey(); return $this->parent->getKey();
} }
/**
* Return update values
*
* @param $array
* @param string $prepend
* @return array
*/
public static function getUpdateValues($array, $prepend = '')
{
$results = [];
foreach ($array as $key => $value) {
$results[$prepend.$key] = $value;
}
return $results;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment