PHP code example of porebskk / simple-php-document-store

1. Go to this page and download the library: Download porebskk/simple-php-document-store 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/ */

    

porebskk / simple-php-document-store example snippets


//Setting up the DbalAdapter, we are using here Sqlite in memory mode
//ger::getConnection($connectionParams);
$conn->exec('PRAGMA foreign_keys = ON');
$dbalAdapter = new DbalAdapter($conn);

//this is only 

$document = [
    'person' => [
        'head' => [
            'nose' => 'big',
            'eyes' => 'blue',
        ],
    ],
];

$documentId = $dbalAdapter->store($document);

$documentId = $dbalAdapter->store($document);

$document = $dbalAdapter->searchById($documentId);

$query = new Query();
//Finding document where the JSON Path "person.head.eyes" is either red or orange
//Allowed operators depend on the adapter implementation
//for DBAL see the ExpressionBuilder::* constants
$query->whereAnd(
    (new OrStatement())->add(
        new WhereStatement('person.head.eyes', '=', 'red'),
        new WhereStatement('person.head.eyes', '=', 'orange')
    )
);

$documents = $dbalAdapter->searchByQuery($query);

//It is possible to wrap AND/OR Statement as deep as possible
$query->whereAnd(
    (new OrStatement())->add(
        new WhereStatement('person.head.eyes', '=', 'blue'),
        (new AndStatement())->add(
            new WhereStatement('person.head.eyes', '=', 'orange'),
            new WhereStatement('person.character.crazy', '=', 'yes')
        )
    ),
    new WhereStatement('person.feet', '=', 'big')
);

$dbalAdapter->update($documentId, $updatedDocument);

$query = (new Query())->limit(5);

$maximumOf5Documents = $dbalAdapter->searchByQuery($query);

$document = [
    'friends' => [
        ['person' => ['name' => 'Bob', 'age' => 26]],
        ['person' => ['name' => 'Alice', 'age' => 25]],
    ],
];

$dbalAdapter->store($document);

//Following paths will be extracted from the array:
//'friends.person.name' => 'Bob',
//'friends.person.age' => 26
//'friends.person.name' => 'Alice'
//'friends.person.age' => 25

//Now we query the data
$query = new Query();
$query->whereAnd(
    new WhereStatement('friends.person.age', '<', '30')
);

$documents = $dbalAdapter->searchByQuery($query);