PHP code example of huseinarbi / bego

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

    

huseinarbi / bego example snippets


/* Query the table */
$results = $music->query()
    ->key('Bob Dylan')
    ->condition(Condition::attribute('SongTitle')->eq('How many roads'))
    ->filter(Condition::attribute('Year')->eq(1966))
    ->fetch(); 

/* Query a global index */
$results = $music->query('My-Global-Index')
    ->key('Bob Dylan')
    ->condition(Condition::attribute('SongTitle')->eq('How many roads'))
    ->filter(Condition::attribute('Year')->eq(1966))
    ->fetch(); 

/* Query a local index */
$results = $music->query('My-Local-Index')
    ->key('Bob Dylan')
    ->condition(Condition::attribute('SongTitle')->eq('How many roads'))
    ->filter(Condition::attribute('Year')->eq(1966))
    ->fetch(); 

/* Execute query and return first page of results */
$results = $music->query()
    ->key('Bob Dylan')
    ->condition(Condition::attribute('SongTitle')->eq('How many roads'))
    ->fetch(); 

foreach ($results as $item) {
    echo "{$item->attribute('Id')}\n";
}

echo "{$results->count()} items in result set\n";
echo "{$results->getScannedCount()} items scanned in query\n";
echo "{$results->getQueryCount()} trips to the database\n";
echo "{$results->getQueryTime()} total execution time (seconds)\n";

$item = $results->first();

$item = $results->last();

//Get the 3rd item
$item = $results->item(3); 

//Extract one attribute from all items
$allTitles = $results->attribute('SongTitle'); 

//Aggregegate one attribute for all items
$totalSales = $results->sum('Sales'); 


$results = $music->query()
    ->key('Bob Dylan')
    ->condition(Condition::attribute('SongTitle')->eq('How many roads'));

/* Option 1: Get one page orf results only (default) */
$results = $query->fetch();

/* Option 2: Execute up to 10 queries */
$results = $query->fetch(10); 

/* Option 3: Get all items in the dataset no matter the cost */
$results = $query->fetch(null);

$results = $music->query()
    ->key('Bob Dylan')
    ->condition(Condition::attribute('SongTitle')->eq('How many roads'));

/* First Hop: Get one page */
$results = $query->fetch(1);
$pointer = $results->getLastEvaluatedKey();

/* Second Hop: Get one more page, continueing from previous request */
$results = $query->fetch(1, $pointer); 

$results = $music->query()
    ->key('Bob Dylan')
    ->condition(Condition::attribute('SongTitle')->eq('How many roads'))
    ->consumption()
    ->fetch();

echo $results->getCapacityUnitsConsumed();

/* Fetch an item */
$item = $music->fetch(
    'Bob Dylan', 'How many roads'
);

if ($item->isEmpty()) {
    throw new \Exception("Requested record could not be found");
}

if ($item->isSet('hit')) {
    echo "{$item->attribute('SongTitle')} is a hit";
}

echo $item->attribute('Id');

/* Return value if attribute exists, otherwise NULL */
echo $item->attribute('Artist');
echo $item->Artist; //shorthand

/* Return value if attribute exists, otherwise throw exception */
echo $item->ping('Artist');

/* Checking if an attribute exists and not empty */
echo $item->isSet('Artist') ? 'Yes' : 'No';
echo isset($item->Artist) ? 'Yes' : 'No'; //shorthand

/* Update an item */
$item->set('Year', 1966);
$item->Year = 1966; //shorthand

$result = $music->update($item);

$results = $music->query()
    ->key('Bob Dylan')
    ->condition(Condition::attribute('SongTitle')->eq('How many roads'))
    ->filter(Condition::attribute('Year')->eq(1966))
    ->fetch(); 

foreach ($results as $item) {
    $item->set('Year', $item->attribute('Year') + 1);
    $music->update($item);
}

/* Update an item */
$item->Year = 1966;

$result = $music->update($item);

$results = $music->query()
    ->key('Bob Dylan')
    ->condition(Condition::attribute('SongTitle')->eq('How many roads'))
    ->filter(Condition::attribute('Year')->eq(1966))
    ->fetch(); 

foreach ($results as $item) {
    $item->Year = $item->Year + 1;
    $music->update($item);
}