Stores

Stores are objects that implement READ/UPDATE/DELETE operations against a storage medium for objects that implement the StorableInterfacearrow-up-right interface. In a nutshell, an object is storable if it has an ID (see the StorableTrait arrow-up-rightand AbstractStorablearrow-up-right for a simple implementation). All classes belonging to the Storage namespace can be found herearrow-up-right.

There are 3 stores that the Foundations Module comes with:

circle-info

Stores are storable as well. It is possible to create a store that stores other stores! Check the MultiStoreAwareInterfacearrow-up-right and MultiStoreAwareTraitarrow-up-right for examples.

Here is a simple example of a dummy storable object stored in a memory store:

<?php

namespace DeepWebSolutions\Plugins\MyTestPlugin\Storage;

use DeepWebSolutions\Framework\Foundations\Utilities\Storage\AbstractStorable;
use DeepWebSolutions\Framework\Foundations\Utilities\Storage\Stores\MemoryStore;

class MyStorable extends AbstractStorable {
    public string $storable_property;
}

$my_storable1 = new MyStorable( 'unique-id-1' );
$my_storable1->storable_property = 'test value 1';

$my_storable2 = new MyStorable( 'unique-id-2' );
$my_storable1->storable_property = 'test value 2';

$memory_store = new MemoryStore( 'my-memory-store' );
$memory_store->add( $my_storable1 );
$memory_store->add( $my_storable2 );

echo $memory_store->count(); // echoes 2
echo $memory_store->get( 'unique-id-1' )->storable_property; // echoes 'test value 1'

$my_storable3 = new MyStorable( 'unique-id-1' ); // same as $my_storable1
$my_storable3->storable_property = 'test value 3';
$memory_store->update( $my_storable3 );

echo $memory_store->get( 'unique-id-1' )->storable_property; // echoes 'test value 3'
triangle-exclamation

Last updated