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

Commit 6b23de6

Browse files
committed
Added #21 for checking database record exists, added has() method on database.
1 parent 2e33986 commit 6b23de6

File tree

5 files changed

+73
-3
lines changed

5 files changed

+73
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
Change Log
22
==========
33

4+
### 08/13/2018 - 1.0.19
5+
* Added #21 for checking database record exists, added `has()` method on database.
6+
47
### 07/14/2018 - 1.0.18
58
* Fixed #17 for PHP 5.6
69
* Replaced the spaceship operators with a php 5.6 alternative.

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ $item->tags = ['php','developer','html5'];
6060
// need to save? thats easy!
6161
$item->save();
6262

63+
64+
// check if a record exists and do something if it does or does not
65+
if ($database->has('kingslayer'))
66+
{
67+
// do some action
68+
}
69+
70+
6371
// Need to find all the users that have a tag for "php" ?
6472
$users = $db->query()->where('tags','IN','php')->results();
6573

@@ -129,6 +137,7 @@ $userId = '92832711';
129137

130138
// get the user information by id
131139
$item = $db->get($userId);
140+
132141
```
133142

134143
`get()` returns `\Filebase\Document` object and has its own methods which you can call.
@@ -205,7 +214,8 @@ Here is a list of methods you can use on the database class.
205214
|Method|Details|
206215
|---|---|
207216
|`version()` | Current version of your Filebase library |
208-
|`get()` | Refer to [get()](https://github.com/filebase/Filebase#3-get-and-methods) |
217+
|`get($id)` | Refer to [get()](https://github.com/filebase/Filebase#3-get-and-methods) |
218+
|`has($id)` | Check if a record exist returning true/false |
209219
|`findAll()` | Returns all documents in database |
210220
|`count()` | Number of documents in database |
211221
|`flush(true)` | Deletes all documents. |

src/Database.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,27 @@ public function get($id)
148148
//--------------------------------------------------------------------
149149

150150

151+
/**
152+
* has
153+
*
154+
* Check if a record already exists
155+
*
156+
* @param mixed $id
157+
*
158+
* @return bool true/false
159+
*/
160+
public function has($id)
161+
{
162+
$format = $this->config->format;
163+
$record = Filesystem::read( $this->config->dir.'/'.Filesystem::validateName($id, $this->config->safe_filename).'.'.$format::getFileExtension() );
164+
165+
return $record ? true : false;
166+
}
167+
168+
169+
//--------------------------------------------------------------------
170+
171+
151172
/**
152173
* backup
153174
*

tests/DocumentTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,42 @@ public function testSave()
3838
//--------------------------------------------------------------------
3939

4040

41+
42+
43+
/**
44+
* testDoesNotExist()
45+
*
46+
* TEST CASE:
47+
* - Save document with data
48+
* - Get the document
49+
* - Check that the data is there and the document exist
50+
*
51+
*/
52+
public function testDoesNotExist()
53+
{
54+
$db = new \Filebase\Database([
55+
'dir' => __DIR__.'/databases',
56+
'cache' => false
57+
]);
58+
59+
$db->flush(true);
60+
61+
// get saved data (put into array)
62+
$doc = $db->get('doesexist')->save(['key'=>'value']);
63+
64+
$this->assertEquals(true, $db->has('doesexist'));
65+
66+
$this->assertEquals(false, $db->has('doesnotexist'));
67+
68+
$db->flush(true);
69+
}
70+
71+
72+
//--------------------------------------------------------------------
73+
74+
75+
76+
4177
/**
4278
* testSetIdGetId()
4379
*

tests/QueryTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ public function testLimitOffset()
369369
*/
370370
public function testSelectQuery()
371371
{
372-
/*$db = new \Filebase\Database([
372+
$db = new \Filebase\Database([
373373
'dir' => __DIR__.'/databases/users_select',
374374
'cache' => false
375375
]);
@@ -416,7 +416,7 @@ public function testSelectQuery()
416416

417417
// select using arrays instead of strings
418418
$test3 = $db->query()->select(['name','email'])->first();
419-
$this->assertEquals(['JR MM','jrmm@email.com'], [$test3['name'],$test3['email']]);*/
419+
$this->assertEquals(['JR MM','jrmm@email.com'], [$test3['name'],$test3['email']]);
420420

421421
// return the "name" (selecting only 1 item)
422422
// currently DOES not work with nested..

0 commit comments

Comments
 (0)