Skip to content

Commit 9807666

Browse files
committed
Soft deletes check in find
1 parent 0b20cfb commit 9807666

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

src/DSL/Bridge.php

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -213,30 +213,39 @@ public function processToDslForSearch($searchParams, $searchOptions, $wheres, $o
213213
/**
214214
* @throws QueryException
215215
*/
216-
public function processGetId($id, $columns)
216+
public function processGetId($id, $columns, $softDeleteColumn): Results
217217
{
218218
$params = [
219219
'index' => $this->index,
220220
'id' => $id,
221221
];
222-
if ($columns && $columns != '*') {
223-
$params['_source'] = $columns;
222+
if (empty($columns)) {
223+
$columns = ['*'];
224224
}
225+
if (! is_array($columns)) {
226+
$columns = [$columns];
227+
}
228+
$allColumns = $columns[0] == '*';
229+
230+
if ($softDeleteColumn && ! $allColumns && ! in_array($softDeleteColumn, $columns)) {
231+
$columns[] = $softDeleteColumn;
232+
}
233+
$params['_source'] = $columns;
225234
$process = [];
226235
try {
227236
$process = $this->client->get($params);
228237
} catch (ClientResponseException $e) {
229238
//if the error is a 404 continue, else throw it
230239
if ($e->getCode() !== 404) {
231-
return $this->_throwError($e, $params, $this->_queryTag(__FUNCTION__));
240+
$this->_throwError($e, $params, $this->_queryTag(__FUNCTION__));
232241
}
233242

234243
} catch (Exception $e) {
235244
//Something else went wrong, throw it
236-
return $this->_throwError($e, $params, $this->_queryTag(__FUNCTION__));
245+
$this->_throwError($e, $params, $this->_queryTag(__FUNCTION__));
237246
}
238247

239-
return $this->_sanitizeGetResponse($process, $params, $this->_queryTag(__FUNCTION__));
248+
return $this->_sanitizeGetResponse($process, $params, $softDeleteColumn, $this->_queryTag(__FUNCTION__));
240249
}
241250

242251
/**
@@ -1126,21 +1135,30 @@ private function _queryTag($function): string
11261135
return str_replace('process', '', $function);
11271136
}
11281137

1129-
public function _sanitizeGetResponse($response, $params, $queryTag)
1138+
public function _sanitizeGetResponse($response, $params, $softDeleteColumn, $queryTag)
11301139
{
11311140
$data['_id'] = $params['id'];
1132-
if (! $response) {
1141+
$softDeleted = false;
1142+
if ($softDeleteColumn) {
1143+
$softDeleted = ! empty($response['_source'][$softDeleteColumn]);
1144+
}
1145+
1146+
if (! $response || $softDeleted) {
11331147
//Was not found
11341148
$result = $this->_return($data, [], $params, $queryTag);
11351149
$result->setError($data['_id'].' not found', 404);
11361150

11371151
return $result;
11381152
}
1153+
11391154
if (! empty($response['_source'])) {
11401155
foreach ($response['_source'] as $key => $value) {
11411156
$data[$key] = $value;
11421157
}
11431158
}
1159+
if ($softDeleteColumn) {
1160+
unset($data[$softDeleteColumn]);
1161+
}
11441162

11451163
return $this->_return($data, [], $params, $queryTag);
11461164
}

src/Eloquent/Builder.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Illuminate\Database\ConnectionInterface;
1010
use Illuminate\Database\Eloquent\Builder as BaseEloquentBuilder;
1111
use Illuminate\Database\Eloquent\ModelNotFoundException;
12+
use Illuminate\Database\Eloquent\SoftDeletingScope;
1213
use Illuminate\Pagination\Cursor;
1314
use Illuminate\Pagination\CursorPaginator;
1415
use Illuminate\Pagination\Paginator;
@@ -166,10 +167,20 @@ public function createWithoutRefresh(array $attributes = []): \Illuminate\Databa
166167

167168
public function find($id, $columns = ['*']): ?Model
168169
{
169-
$find = $this->query->find($id, $columns);
170+
$softDeleteColumn = null;
171+
if (method_exists($this->model, 'getQualifiedDeletedAtColumn')) {
172+
$softDeleteColumn = $this->model->getQualifiedDeletedAtColumn();
173+
if (in_array(SoftDeletingScope::class, $this->removedScopes)) {
174+
$softDeleteColumn = null;
175+
}
176+
}
177+
$find = $this->query->find($id, $columns, $softDeleteColumn);
170178
if ($find->isSuccessful()) {
171-
$model = $this->newModelInstance($find->data);
179+
$instance = $this->newModelInstance();
180+
$model = $instance->newFromBuilder($find->data);
172181
$model->setMeta($find->getMetaDataAsArray());
182+
$model->setRecordIndex($find->getMetaData()->getIndex());
183+
$model->setIndex($find->getMetaData()->getIndex());
173184

174185
return $model;
175186
}

src/Query/Builder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,9 @@ public function value($column)
189189
/**
190190
* {@inheritdoc}
191191
*/
192-
public function find($id, $columns = []): Results
192+
public function find($id, $columns = [], $softDeleteColumn = null): Results
193193
{
194-
return $this->connection->getId($id, $columns);
194+
return $this->connection->getId($id, $columns, $softDeleteColumn);
195195
}
196196

197197
/**

0 commit comments

Comments
 (0)