PHP code example of phuze / php-cosmos

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

    

phuze / php-cosmos example snippets


$conn = new \Phuze\PhpCosmos\CosmosDb('host', 'key');
$conn->setHttpClientOptions(['verify' => false]); # optional: set guzzle client options.
$db = $conn->selectDB('databaseName');
$collection = $db->selectCollection('collectionName');

# if a collection does not exist, it will be created when you
# attempt to select the collection. however, if you have created
# your database with shared throughput, then all collections 

# consider a existing collection called "Users" with a partition key "country"

# insert a record
$rid = \Phuze\PhpCosmos\QueryBuilder::instance()
    ->setCollection($collection)
    ->setPartitionKey('country')
    ->save(['id' => '1', 'name' => 'John Doe', 'age' => 22, 'country' => 'Portugal']);

# insert a record against a collection with a nested partition key
$rid = \Phuze\PhpCosmos\QueryBuilder::instance()
    ->setCollection($collection)
    ->setPartitionKey('billing.country')
    ->save([
        'id' => '2',
        'name' => 'Jane Doe',
        'age' => 35,
        'billing' => ['country' => 'Portugal']
    ]);

# update a record
$rid = \Phuze\PhpCosmos\QueryBuilder::instance()
    ->setCollection($collection)
    ->setPartitionKey('country')
    ->save([
        '_rid'    => $rid, // existing document _rid
        'id'      => '2',
        'name'    => 'Jane Doe',
        'age'     => 36,
        'country' => 'Portugal'
    ]);

# query a document and return it as an array
$res = \Phuze\PhpCosmos\QueryBuilder::instance()
    ->setCollection($collection)
    ->select("c.id, c.name")
    ->where("c.age > @age and c.country = @country")
    ->params(['@age' => 30, '@country' => 'Portugal'])
    ->find(true)
    ->toArray();

# query a document using a known partition value,
# and return as an array. note: setting a known
# partition value will result in a more efficient
# query against your database as it will not rely
# on cross-partition querying.
$res = \Phuze\PhpCosmos\QueryBuilder::instance()
    ->setCollection($collection)
    ->setPartitionKey('country')
    ->setPartitionValue('Portugal')
    ->select("c.id, c.name")
    ->where("c.age > @age and c.country = @country")
    ->params(['@age' => 30, '@country' => 'Portugal'])
    ->find()
    ->toArray();

# query the top 5 documents as an array, with the
# document ID as the array key.
$res = \Phuze\PhpCosmos\QueryBuilder::instance()
    ->setCollection($collection)
    ->select("c.id, c.username")
    ->where("c.age > @age and c.country = @country")
    ->params(['@age' => 10, '@country' => 'Portugal'])
    ->limit(5)
    ->findAll(true)
    ->toArray('id');

# query a document using a collection alias and cross partition query
$res = \Phuze\PhpCosmos\QueryBuilder::instance()
    ->setCollection($collection)
    ->select("HelloWorld.id, HelloWorld.name")
    ->from("HelloWorld")
    ->where("HelloWorld.age > 30")
    ->findAll(true)
    ->toArray();

# delete one document that matches criteria (single partition)
$res = \Phuze\PhpCosmos\QueryBuilder::instance()
    ->setCollection($collection)
    ->setPartitionKey('country')
    ->where("c.age > 30 and c.country = 'Portugal'")
    ->delete();

# delete all documents that match criteria (cross partition)
$res = \Phuze\PhpCosmos\QueryBuilder::instance()
    ->setCollection($collection)
    ->setPartitionKey('country')
    ->where("c.age > 20")
    ->deleteAll(true);
bash
composer