Skip to content

Commit c5e6a1d

Browse files
committed
Call find() if conditions contain key
Closes #12
1 parent d4ae9f2 commit c5e6a1d

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

src/DynamoDbModel.php

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ protected static function getInstance()
9191
if (is_null(static::$instance)) {
9292
static::$instance = new static;
9393
}
94-
94+
9595
return static::$instance;
9696
}
9797

@@ -272,6 +272,12 @@ public function get($columns = [])
272272

273273
protected function getAll($columns = [], $limit = -1)
274274
{
275+
if ($conditionValue = $this->conditionsContainKey()) {
276+
$item = $this->find($conditionValue, $columns);
277+
278+
return new Collection([$item]);
279+
}
280+
275281
$query = [
276282
'TableName' => $this->getTable(),
277283
];
@@ -289,7 +295,7 @@ protected function getAll($columns = [], $limit = -1)
289295
// If the $where is not empty, we run getIterator.
290296
if (!empty($this->where)) {
291297

292-
// Primary key or index key condition exists, then use Query instead of Scan.
298+
// Index key condition exists, then use Query instead of Scan.
293299
// However, Query only supports a few conditions.
294300
if ($key = $this->conditionsContainIndexKey()) {
295301
$condition = array_get($this->where, "$key.ComparisonOperator");
@@ -317,6 +323,41 @@ protected function getAll($columns = [], $limit = -1)
317323
return new Collection($results);
318324
}
319325

326+
/**
327+
* Check if conditions "where" contain primary key or composite key.
328+
* For composite key, it will return false if the conditions don't have all composite key.
329+
*
330+
* @return array|bool the condition value
331+
*/
332+
protected function conditionsContainKey()
333+
{
334+
if (empty($this->where)) {
335+
return false;
336+
}
337+
338+
$conditionKeys = array_keys($this->where);
339+
340+
$keys = $this->hasCompositeKey() ? $this->compositeKey : [$this->getKeyName()];
341+
342+
$conditionsContainKey = count(array_intersect($conditionKeys, $keys)) === count($keys);
343+
344+
if (!$conditionsContainKey) {
345+
return false;
346+
}
347+
348+
$conditionValue = [];
349+
350+
foreach ($keys as $key) {
351+
$condition = $this->where[$key];
352+
353+
$value = $this->unmarshalItem(array_get($condition, 'AttributeValueList'))[0];
354+
355+
$conditionValue[$key] = $value;
356+
}
357+
358+
return $conditionValue;
359+
}
360+
320361
protected function conditionsContainIndexKey()
321362
{
322363
if (empty($this->where)) {

tests/DynamoDbCompositeModelTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,22 @@ public function testDeleteRecord()
116116
$this->assertArrayNotHasKey('Item', $record);
117117
}
118118

119+
public function testLookingUpByKey()
120+
{
121+
$this->seed();
122+
123+
$item = $this->seed();
124+
125+
$foundItems = $this->testModel
126+
->where('id', $item['id']['S'])
127+
->where('id2', $item['id2']['S'])
128+
->get();
129+
130+
$this->assertEquals(1, $foundItems->count());
131+
132+
$this->assertEquals($this->testModel->unmarshalItem($item), $foundItems->first()->toArray());
133+
}
134+
119135
protected function seed($attributes = [])
120136
{
121137
$item = [

tests/DynamoDbModelTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,19 @@ public function testChainedMethods()
222222
$this->assertEquals($this->testModel->unmarshalItem($secondItem), $foundItems->first()->toArray());
223223
}
224224

225+
public function testLookingUpByKey()
226+
{
227+
$this->seed();
228+
229+
$item = $this->seed();
230+
231+
$foundItems = $this->testModel->where('id', $item['id']['S'])->get();
232+
233+
$this->assertEquals(1, $foundItems->count());
234+
235+
$this->assertEquals($this->testModel->unmarshalItem($item), $foundItems->first()->toArray());
236+
}
237+
225238
protected function seed($attributes = [])
226239
{
227240
$item = [

0 commit comments

Comments
 (0)