PHP code example of naucon / storage

1. Go to this page and download the library: Download naucon/storage 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/ */

    

naucon / storage example snippets


    class Product implements \Serializable
    {
        protected $id;
        protected $sku;
        protected $description;
    
        public function getId()
        {
            return $this->id;
        }
    
        public function setId($id)
        {
            $this->id = $id;
        }
    
        public function getSku()
        {
            return $this->sku;
        }
    
        public function setSku($sku)
        {
            $this->sku = $sku;
        }
    
        public function getDescription()
        {
            return $this->description;
        }
    
        public function setDescription($description)
        {
            $this->description = $description;
        }
    }

    use Naucon\Storage\Provider\ArrayStorage;
    use Naucon\Storage\StorageManager;
        
    $adapter = new ArrayStorage(Product::class);
    $manager = new StorageManager($adapter);

    $model = new Product();
    $model->setId(1);
    $model->setSku('U123');
    $model->setDescription('Dragon fruit');

    $manager->flush(1, $model);

    $model = $manager->create();
    $model->setId(1);
    $model->setSku('U123');
    $model->setDescription('Dragon fruit');

    $manager->flush(1, $model);

    if ($manager->has(1)) {
        $model = $manager->find(1);
    }

    $model = $manager->findOrCreate(1);

    $manager->remove(1, $product);

    $models = $manager->removeAll();

    $models = $manager->findAll();

    $models = $manager->findMultiple([1, 2]);

    
    class Product
    {
        /**
         * @Id
         * @GeneratedValue(strategy="NONE")
         */
        protected $id = null;
    }

    $manager->flush(['product_id' => 1, 'variation_id' => 4], $model);

    $storageInMemory = new ArrayStorage(Product::class);
    $storageSession  = new NativeSessionStorage(Product::class);
    
    $storageChain = new StorageChain();
    $storageChain->register('product_default', $storageInMemory);
    $storageChain->register('product_session', $storageSession);

    $storage = new ArrayStorage(Product::class);
    
    $storageFactory = new StorageFactory();
    $storageFactory->register('product', $storage);
    
    $storage = $storageFactory->getStorage('product');

    $storageInMemory = new ArrayStorage(Product::class);
    $storageSession  = new NativeSessionStorage(Product::class);
    
    $storageChain = new StorageChain();
    $storageChain->register('product_default', $storageInMemory);
    $storageChain->register('product_session', $storageSession);
    
    $storageManager = new StorageManager($storageChain);

    $storageFactory = new StorageFactory();
    $storageFactory->register('product', $storageManager);

    http://127.0.0.1:3000/index.html
bash
    cd examples
    php -S 127.0.0.1:3000