PHP code example of commandstring / jsondb

1. Go to this page and download the library: Download commandstring/jsondb library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

commandstring / jsondb example snippets




use CommandString\JsonDb\Structure\Database;
use CommandString\JsonDb\Structure\Table;

class YourDatabase extends Database {
    protected static array $tableClasses = [];
}



use CommandString\JsonDb\Structure\Table;

class YourTable extends Table {
    protected static string $name = "yourtable";
    protected static string $fileLocation = __DIR__."/yourtable.json";
    protected static array $columns = [];
}

// ...
protected static array $tableClasses = [YourDatabase::class];
// ...


use CommandString\JsonDb\Structure\Column;
use CommandString\JsonDb\Structure\DataTypes;

class YourColumn extends Column {
    protected static DataTypes              $type = DataTypes::STRING;
    protected static string                 $name = "yourcolumn";
    protected static array                  $enumValues = [];
    protected static string|int|float|null  $default;
    protected static bool                   $nullable = false;
    protected static bool                   $unique = false;
}

$db = new YourDatabase; // you can also pass JSON encoder flags into the constructor

$row = $db->yourtable->newRow();

$row->setColumn($db->yourtable::COLUMN_NAME, "<value>") // you can pass a string for the column name but I recommend having constants has mentioned earlier

$row = $row->store(); // returns that same row but this is associated to specific point in the JSON file

/**
 * @var Row[]
 */
$results = $db->yourtable->newQuery()->whereAnd($db->yourtable::COLUMN_NAME, Operators::EQUAL_TO, "<value>")->execute();

$row = $results[0];

$row->setColumn($db->yourtable::COLUMN_NAME, "<new value>");

$row->store(); // updated the row