From 57f5420e60fa10ca1ad96cd60329a2b86268f869 Mon Sep 17 00:00:00 2001 From: Dimitri Date: Thu, 21 Jul 2022 11:30:52 +1000 Subject: [PATCH 1/3] feat(model-update): Modify upsert to return an entity if a key has been found - this also allows for Laravel Models to be updated via Model->save(); --- src/Helpers/QueryBuilderHelper.php | 31 +++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/Helpers/QueryBuilderHelper.php b/src/Helpers/QueryBuilderHelper.php index 9b14e0e..77d7773 100644 --- a/src/Helpers/QueryBuilderHelper.php +++ b/src/Helpers/QueryBuilderHelper.php @@ -116,7 +116,6 @@ public function delete($key = null) return $this->getClient()->deleteBatch($keys); } - /** * @inheritdoc */ @@ -149,11 +148,21 @@ public function insert(array $values, $key = '', $options = []) return null; } + /** + * @inheritdoc + */ + public function update(array $values, $key = '', $options = []) + { + $this->applyBeforeQueryCallbacks(); + return $this->upsert($values, $key, $options); + } + /** * @inheritdoc */ public function upsert(array $values, $key = '', $options = []) { + if (empty($this->from)) { throw new \LogicException('No kind/table specified'); } @@ -161,10 +170,22 @@ public function upsert(array $values, $key = '', $options = []) if (empty($values)) { return true; } - - $key = $key ? $this->getClient()->key($this->from, $key) : $this->getClient()->key($this->from); - - $entity = $this->getClient()->entity($key, $values, $options); + foreach($this->wheres as $where): + if($where['value'] instanceof \Google\Cloud\Datastore\Key): + $key = $where['value']; + break; + endif; + endforeach; + + if($key instanceof \Google\Cloud\Datastore\Key): + $entity = $this->getClient()->lookup($key); + foreach($values as $key=>$value): + $entity->$key = $value; + endforeach; + else: + $key = $key ? $this->getClient()->key($this->from, $key) : $this->getClient()->key($this->from); + $entity = $this->getClient()->entity($key, $values, $options); + endif; return $this->getClient()->upsert($entity); } From d4d57071c681f84894eaed89969f7fd56000a61e Mon Sep 17 00:00:00 2001 From: Dimitri Date: Thu, 21 Jul 2022 11:42:00 +1000 Subject: [PATCH 2/3] fix: add a null check for entity lookup --- src/Helpers/QueryBuilderHelper.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Helpers/QueryBuilderHelper.php b/src/Helpers/QueryBuilderHelper.php index 77d7773..21e7dd1 100644 --- a/src/Helpers/QueryBuilderHelper.php +++ b/src/Helpers/QueryBuilderHelper.php @@ -179,6 +179,11 @@ public function upsert(array $values, $key = '', $options = []) if($key instanceof \Google\Cloud\Datastore\Key): $entity = $this->getClient()->lookup($key); + + if($entity == null): + $entity = $this->getClient()->entity($key, $values, $options); + endif; + foreach($values as $key=>$value): $entity->$key = $value; endforeach; From 910db893a835f5fb738b878b0db5a845ca906a4a Mon Sep 17 00:00:00 2001 From: Dimitri Date: Thu, 21 Jul 2022 11:44:25 +1000 Subject: [PATCH 3/3] refactor: use null coalescing operator --- src/Helpers/QueryBuilderHelper.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Helpers/QueryBuilderHelper.php b/src/Helpers/QueryBuilderHelper.php index 21e7dd1..78e4b3c 100644 --- a/src/Helpers/QueryBuilderHelper.php +++ b/src/Helpers/QueryBuilderHelper.php @@ -178,11 +178,7 @@ public function upsert(array $values, $key = '', $options = []) endforeach; if($key instanceof \Google\Cloud\Datastore\Key): - $entity = $this->getClient()->lookup($key); - - if($entity == null): - $entity = $this->getClient()->entity($key, $values, $options); - endif; + $entity = $this->getClient()->lookup($key) ?? $this->getClient()->entity($key, $values, $options); foreach($values as $key=>$value): $entity->$key = $value;