philwc/jsondb

使用JSON的数据库

维护者

详细信息

github.com/philwc/JsonDB

主页

源代码

安装: 152

依赖: 0

建议者: 0

安全: 0

星标: 8

关注者: 2

分支: 47

v0.2 2016-09-13 07:08 UTC

This package is not auto-updated.

Last update: 2024-09-28 17:38:26 UTC


README

"像一个非常简单的数据库一样处理JSON文件。适用于小型Ajax应用程序。"

示例

test.json

[
{"ID": 0, "Name": "Josef Brunzer", "Age": 43},
{"ID": 1, "Name": "Harald Beidlpraka", "Age": 34},
{"ID": 2, "Name": "Heinz Goschnfuada", "Age": 67},
{"ID": 3, "Name": "Gerald Ofnsacka", "Age": 43}
]

test.php

require '../vendor/autoload.php';
$db     = new \philwc\JsonDB('./data/');
$result = $db->select('test', 'Age', 43);
var_dump($result);

结果

array(2) {
    [0]=> array(3) { ["ID"]=> int(0) ["Name"]=> string(13) "Josef Brunzer" ["Age"]=> int(43) }
    [1]=> array(3) { ["ID"]=> int(3) ["Name"]=> string(15) "Gerald Ofnsacka" ["Age"]=> int(43) }
}

方法概述

JsonDB->select ( "table", "key", "value" ) //Selects multiple lines which contains the key/value and returns it as array
JsonDB->selectAll ( "table" ) //Returns the entire file as array
JsonDB->update ( "table", "key", "value", ARRAY ) //Replaces the line which corresponds to the key/value with the array-data
JsonDB->updateAll ( "table", ARRAY ) //Replaces the entire file with the array-data
JsonDB->insert ( "table", ARRAY ) //Appends a row, returns true on success
JsonDB->delete ( "table", "key", "value" ) //Deletes all lines which corresponds to the key/value, returns number of deleted lines
JsonDB->deleteAll ( "table" ) //Deletes the whole data, returns "true" on success
JsonDB->setPrettyOutput ( "table", bool ) //Set Pretty Output for json_encode
JsonDB->setJsonEncodeOptions ( "table", int ) //Set options for json_encode
JsonDB->setExtension( "newExtension" ) //Set the file extension to use

如果你只用一个JSON文件来存储数据,你也可以使用JsonTable

$db     = new \philwc\JsonTable('./data/test.json');
$result = $db->select('Age', 43);
var_dump($result);

在这种情况下,你不必总是指定表名。

测试位于 /tests 目录中。运行 phpunit 来执行。