Skip to content
This repository was archived by the owner on Jun 2, 2024. It is now read-only.

Commit a012b72

Browse files
Merge pull request #50 from faryar76/1.0
Fixed bug #41
2 parents e847412 + d7630c4 commit a012b72

File tree

2 files changed

+54
-16
lines changed

2 files changed

+54
-16
lines changed

src/QueryLogic.php

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,29 @@ public function __construct(Database $database)
4242
}
4343
}
4444

45+
private function loadDocuments()
46+
{
47+
$predicates = $this->predicate->get();
48+
49+
if ($this->cache===false)
50+
{
51+
$this->documents = $this->database->findAll(true,false);
52+
return $this;
53+
}
54+
55+
$this->cache->setKey(json_encode($predicates));
56+
57+
if ($cached_documents = $this->cache->get())
58+
{
59+
$this->documents = $cached_documents;
60+
61+
$this->sort();
62+
$this->offsetLimit();
63+
return $this;
64+
}
65+
$this->documents = $this->database->findAll(true,false);
66+
return $this;
67+
}
4568
/**
4669
* run
4770
*
@@ -57,22 +80,7 @@ public function run()
5780
$predicates = 'findAll';
5881
}
5982

60-
if ($this->cache !== false)
61-
{
62-
$this->cache->setKey(json_encode($predicates));
63-
64-
if ($cached_documents = $this->cache->get())
65-
{
66-
$this->documents = $cached_documents;
67-
68-
$this->sort();
69-
$this->offsetLimit();
70-
71-
return $this;
72-
}
73-
}
74-
75-
$this->documents = $this->database->findAll(true,false);
83+
$this->loadDocuments();
7684

7785
if ($predicates !== 'findAll')
7886
{

tests/DatabaseTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,4 +308,34 @@ public function test_must_return_exception_on_non_exist_method()
308308
$this->expectException(\BadMethodCallException::class);
309309
$results = $db->none('name','=','John')->andWhere('email','==','john@example.com')->resultDocuments();
310310
}
311+
/**
312+
* based on issue #41
313+
* results() returns document instead of array #41
314+
*/
315+
public function test_must_return_array_on_select_an_culomn_from_cache()
316+
{
317+
$db = new \Filebase\Database([
318+
'dir' => __DIR__.'/databases/saved',
319+
'cache' => true
320+
]);
321+
322+
$db->flush(true);
323+
324+
for ($x = 1; $x <= 10; $x++)
325+
{
326+
$user = $db->get(uniqid());
327+
$user->name = 'John';
328+
$user->email = 'john@example.com';
329+
$user->save();
330+
}
331+
332+
$db->where('name','=','John')->andWhere('email','==','john@example.com')->select('email')->results();
333+
$result_from_cache = $db->where('name','=','John')->andWhere('email','==','john@example.com')->select('email')->results();
334+
335+
$this->assertCount(10,$result_from_cache);
336+
$this->assertEquals(['email'=>'john@example.com'],$result_from_cache[0]);
337+
$this->assertInternalType('array', $result_from_cache[0]);
338+
$this->assertInternalType('string', $result_from_cache[0]['email']);
339+
$db->flush(true);
340+
}
311341
}

0 commit comments

Comments
 (0)