Skip to content

Commit

Permalink
Call find() if conditions contain key
Browse files Browse the repository at this point in the history
Closes #12
  • Loading branch information
baopham committed Apr 9, 2016
1 parent d4ae9f2 commit c5e6a1d
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
45 changes: 43 additions & 2 deletions src/DynamoDbModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ protected static function getInstance()
if (is_null(static::$instance)) {
static::$instance = new static;
}

return static::$instance;
}

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

protected function getAll($columns = [], $limit = -1)
{
if ($conditionValue = $this->conditionsContainKey()) {
$item = $this->find($conditionValue, $columns);

return new Collection([$item]);
}

$query = [
'TableName' => $this->getTable(),
];
Expand All @@ -289,7 +295,7 @@ protected function getAll($columns = [], $limit = -1)
// If the $where is not empty, we run getIterator.
if (!empty($this->where)) {

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

/**
* Check if conditions "where" contain primary key or composite key.
* For composite key, it will return false if the conditions don't have all composite key.
*
* @return array|bool the condition value
*/
protected function conditionsContainKey()
{
if (empty($this->where)) {
return false;
}

$conditionKeys = array_keys($this->where);

$keys = $this->hasCompositeKey() ? $this->compositeKey : [$this->getKeyName()];

$conditionsContainKey = count(array_intersect($conditionKeys, $keys)) === count($keys);

if (!$conditionsContainKey) {
return false;
}

$conditionValue = [];

foreach ($keys as $key) {
$condition = $this->where[$key];

$value = $this->unmarshalItem(array_get($condition, 'AttributeValueList'))[0];

$conditionValue[$key] = $value;
}

return $conditionValue;
}

protected function conditionsContainIndexKey()
{
if (empty($this->where)) {
Expand Down
16 changes: 16 additions & 0 deletions tests/DynamoDbCompositeModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,22 @@ public function testDeleteRecord()
$this->assertArrayNotHasKey('Item', $record);
}

public function testLookingUpByKey()
{
$this->seed();

$item = $this->seed();

$foundItems = $this->testModel
->where('id', $item['id']['S'])
->where('id2', $item['id2']['S'])
->get();

$this->assertEquals(1, $foundItems->count());

$this->assertEquals($this->testModel->unmarshalItem($item), $foundItems->first()->toArray());
}

protected function seed($attributes = [])
{
$item = [
Expand Down
13 changes: 13 additions & 0 deletions tests/DynamoDbModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,19 @@ public function testChainedMethods()
$this->assertEquals($this->testModel->unmarshalItem($secondItem), $foundItems->first()->toArray());
}

public function testLookingUpByKey()
{
$this->seed();

$item = $this->seed();

$foundItems = $this->testModel->where('id', $item['id']['S'])->get();

$this->assertEquals(1, $foundItems->count());

$this->assertEquals($this->testModel->unmarshalItem($item), $foundItems->first()->toArray());
}

protected function seed($attributes = [])
{
$item = [
Expand Down

0 comments on commit c5e6a1d

Please sign in to comment.