PHP code example of jorisvaesen / cakephp-keyvalue-pairs

1. Go to this page and download the library: Download jorisvaesen/cakephp-keyvalue-pairs 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/ */

    

jorisvaesen / cakephp-keyvalue-pairs example snippets


Plugin::load('JorisVaesen/KeyValuePairs');

$this->addBehavior('JorisVaesen/KeyValuePairs.KeyValuePairs', [
    // Here you can override the default options
]);

Cache::config('pairs', [
    'className' => 'File',
    'duration' => '+999 days',  // cache gets invalidated automatically when a pair is saved or removed
    'path' => CACHE,
    'prefix' => 'pairs_'
]);

public function initialize(array $config) 
{
    ...
    
    $this->addBehavior('JorisVaesen/KeyValuePairs.KeyValuePairs', [
        'fields' => [               //  We just leave this the default
            'key' => 'key',
            'value' => 'value'
        ],
        'cache' => true,            //  Enable caching
        'cacheConfig' => 'pairs',   //  Tell the plugin to use the pairs cache config
        'scope' => [                //  Just as example to show how to use extra conditions when fetching pairs
            'is_deleted' => false
        ],
        'preventDeletion' => true,  //  Prevents us from deleting any record in this table (and thereby possibly break the app)
        'allowedKeys' => [          //  Prevents us from saving any other key than the ones specified here
            'invoice_prefix',
            'invoice_next_number'
        ]
    ]);
}

public function add() 
{
    ...
    
    $pairsTable = TableRegistry::get('Configs');
    // We set $$pairs) {
        // throw error
    }
    
    $invoice->number = $pairs['invoice_prefix']->value . $pairs['invoice_next_number']->value;
    
    if ($this->Invoices->save($invoice)) {
        $pairs['invoice_next_number']->value = (int)$pairs['invoice_next_number']->value + 1; 
        $pairsTable->save($pairs['invoice_next_number']);
    }
    
    ...
}