Data

Bear Framework provides you with a file-based object storage. It's called Object Storage and is integrated into the framework for easy access. Object/files are accessed by key (internally it's a filename).

One of the great benefits of this library is that it support atomic operations (files locking is used).

Additionally, you can set metadata to each object. It's a great way to store size, dates or other file metadata.

And of course, the data can be backed up easily. It is just raw files on your hard drive.

You can mark an object as public and get it's public URL by calling $app->data->getFilename and passing it to $app->assets->getUrl.

API

BearFramework\App\Data - Data storage
BearFramework\App\Data::append - Appends data to the object specified. If the object does not exist, it will be created.
BearFramework\App\Data\DataLockedException
BearFramework\App\Data\DataLockedException::__construct - Construct the exception
BearFramework\App\Data\DataLockedException::getCode
BearFramework\App\Data\DataLockedException::getFile
BearFramework\App\Data\DataLockedException::getLine
BearFramework\App\Data\DataLockedException::getMessage
BearFramework\App\Data\DataLockedException::getPrevious
BearFramework\App\Data\DataLockedException::getTrace
BearFramework\App\Data\DataLockedException::getTraceAsString
BearFramework\App\Data\DataLockedException::__toString
BearFramework\App\Data\DataLockedException::__wakeup
BearFramework\App\Data::delete - Deletes the object specified and it's metadata
BearFramework\App\Data::duplicate - Creates a copy of the object specified
BearFramework\App\Data::execute - Executes multiple commands
BearFramework\App\Data::get - Retrieves object data for specified key
BearFramework\App\Data::getFilename - Returns the filename of the object key specified
BearFramework\App\Data::getKeyFromFilename - Returns the key name of the object filename specified
BearFramework\App\Data::isPublic - Checks if an object is marked as public
BearFramework\App\Data::isValidKey - Checks if an key is valid
BearFramework\App\Data::makePrivate - Marks object as private, so it cannot be accessed as an asset
BearFramework\App\Data::makePublic - Marks object as public so it can be accessed as an asset
BearFramework\App\Data::rename - Changes the key of the object specified
BearFramework\App\Data::search - Searches for items
BearFramework\App\Data::set - Saves data

Examples

Setting data

$app->data->set([
    'key' => 'users/1',
    'body' => '{"name":"John Smith","email":"john@example.com"}',
    'metadata.lastAccessTime' => '1234567890'
]);

Getting data

$result = $app->data->get([
    'key' => 'users/1',
    'result' => ['body', 'metadata']
]);
/* If the object exists $result will be: Array (     [body] => {"name":"John Smith","email":"john@example.com"}     [metadata.lastAccessTime] => 1234567890 ) If the object does not exists $result will empty array. */

Appending data

$app->data->append([
    'key' => 'visits/ip.log',
    'body' => "123.123.123.123\n"
]);

Duplicating data

$app->data->duplicate([
    'sourceKey' => 'users/1',
    'targetKey' => 'users/2'
]);

Renaming data

$app->data->rename([
    'sourceKey' => 'users/2',
    'targetKey' => 'users/3'
]);

Deleting data

$app->data->delete([
    'key' => 'users/3'
]);

Searching for data

$result = $app->data->search([
    'where' => [
        ['key', ['users/1']]
    ],
    'result' => ['key', 'body']
]);
/* The result is array of arrays containing the matching objects Array (     [0] => Array         (             [key] => users/1             [body] => {"name":"John Smith","email":"john@example.com"}         ) ) */

//If there are not matches an empty array is returned
$result = $app->data->search([
    'where' => [
        ['key', 'users/9']
    ],
    'result' => ['key', 'body']
]);

// You can also use regular expresions
$result = $app->data->search([
    'where' => [
        ['key', '^users\/', 'regexp']
    ],
    'result' => ['key', 'body']
]);
Next: Addons