1. Go to this page and download the library: Download ravendb/ravendb-php-client 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/ */
ravendb / ravendb-php-client example snippets
use RavenDB\Documents\DocumentStore;
$store = new DocumentStore('http://live-test.ravendb.net', 'databaseName');
$store->initialize();
$session = $store->openSession();
$user = $session->load('users/1-A'); // Load document
$user->setPassword(PBKDF2('new password')); // Update data
$session->saveChanges(); // Save changes
// Data is now persisted
// You can proceed e.g. finish web request
$query
->waitForNonStaleResults()
->usingDefaultOperator('AND')
->whereEquals('manufacturer', 'Apple')
->whereEquals('in_stock', true)
->whereBetween('last_update', new DateTime('- 1 week'), new DateTime())
->orderBy('price');
$results = $query->toList(); // get all results
// ...
$firstResult = $query->first(); // gets first result
// ...
$single = $query->single(); // gets single result
// RQL
// from users select name
// Query
$userNames = $session->query(User::class)
->selectFields("name")
->toList();
// Sample results
// John, Stefanie, Thomas
// RQL
// from users order by age
// Query
$session->query(User::class)
->orderBy("age")
->take(2) // only the first 2 entries will be returned
->toList();
// Sample results
// [ User {
// name: 'Stefanie',
// age: 25,
// registeredAt: 2015-07-29T22:00:00.000Z,
// id: 'users/2-A' },
// User {
// name: 'Thomas',
// age: 25,
// registeredAt: 2016-04-24T22:00:00.000Z,
// id: 'users/3-A' } ]
// RQL
// from users order by age
// Query
$session->query(User::class)
->orderBy("age")
->take(1) // return only 1 result
->skip(1) // skip the first result, return the second result
->toList();
// Sample results
// [ User {
// name: 'Thomas',
// age: 25,
// registeredAt: 2016-04-24T22:00:00.000Z,
// id: 'users/3-A' } ]
$doc = new User();
$doc->setName('John');
// Store a document, the entity will be tracked.
$session->store($doc);
// Get read stream or buffer to store
$fileStream = file_get_contents("../photo.png");
// Store attachment using entity
$session->advanced()->attachments()->store($doc, "photo.png", $fileStream, "image/png");
// OR store attachment using document ID
$session->advanced()->attachments()->store($doc->getId(), "photo.png", $fileStream, "image/png");
// Persist all changes
$session->saveChanges();
// Get an attachment
$attachment = $session->advanced()->attachments()->get($documentId, "photo.png")
// Attachment.details contains information about the attachment:
// {
// name: 'photo.png',
// documentId: 'users/1-A',
// contentType: 'image/png',
// hash: 'MvUEcrFHSVDts5ZQv2bQ3r9RwtynqnyJzIbNYzu1ZXk=',
// changeVector: '"A:3-K5TR36dafUC98AItzIa6ow"',
// size: 4579
// }
// Attachment.data is a Readable.
$fileBytes = $attachment->getData();
file_put_contents('../photo.png', $fileBytes);
// Use a loaded entity to determine attachments' names
$session->advanced()->attachments()->getNames($doc);
// Sample results:
// [ { name: 'photo.png',
// hash: 'MvUEcrFHSVDts5ZQv2bQ3r9RwtynqnyJzIbNYzu1ZXk=',
// contentType: 'image/png',
// size: 4579 } ]
$session = $store->openSession();
// Create a document with time series
$session->store(new User(), "users/1");
$tsf = $session->timeSeriesFor("users/1", "heartbeat");
// Append a new time series entry
$tsf->append(new DateTime(), 120);
$session->saveChanges();
$session = $store->openSession();
// Get time series for document by time series name
$tsf = $session->timeSeriesFor("users/1", "heartbeat");
// Get all time series entries
$heartbeats = $tsf->get();
$user = new User();
$user->setName("Marcin");
$user->setAge(30);
$user->setPet("Cat");
$session = $store->openSession();
// Store a document
$session->store($user, "users/1");
$session->saveChanges();
// Modify the document to create a new revision
$user->setName("Roman");
$user->setAge(40);
$session->saveChanges();
// Get revisions
$revisions = $session->advanced()->revisions()->getFor("users/1");
// Sample results:
// [ { name: 'Roman',
// age: 40,
// pet: 'Cat',
// '@metadata': [Object],
// id: 'users/1' },
// { name: 'Marcin',
// age: 30,
// pet: 'Cat',
// '@metadata': [Object],
// id: 'users/1' }
// ]
// Some documents in users collection with misspelled name term
// [ User {
// name: 'Johne',
// age: 30,
// ...
// id: 'users/1-A' },
// User {
// name: 'Johm',
// age: 31,
// ...
// id: 'users/2-A' },
// User {
// name: 'Jon',
// age: 32,
// ...
// id: 'users/3-A' },
// ]
// Static index definition
class UsersIndex extends AbstractJavaScriptIndexCreationTask {
public function __construct() {
parent::__construct();
$this->map = "from user in docs.users select new { user.name }";
// Enable the suggestion feature on index-field 'name'
$this->suggestion("name");
}
}
// ...
$session = $store->openSession();
// Query for similar terms to 'John'
// Note: the term 'John' itself will Not be part of the results
$suggestedNameTerms = $session->query(User::class, UsersIndex::class)
->suggestUsing(function($x) { return $x->byField("name", "John"); })
->execute();
// Sample results:
// { name: { name: 'name', suggestions: [ 'johne', 'johm', 'jon' ] } }
// Increment 'age' field by 1
$session->advanced()->increment("users/1", "age", 1);
// Set 'underAge' field to false
$session->advanced->patch("users/1", "underAge", false);
$session->saveChanges();
class Product {
public ?string $id = null,
public string $title = '',
public int $price = 0,
public string $currency = 'USD',
public int $storage = 0,
public string $manufacturer = '',
public bool $in_stock = false,
public ?DateTime $last_update = null
public function __construct(
$id = null,
$title = '',
$price = 0,
$currency = 'USD',
$storage = 0,
$manufacturer = '',
$in_stock = false,
$last_update = null
) {
$this->id = $id;
$this->title = $title;
$this->price = $price;
$this->currency = $currency;
$this->storage = $storage;
$this->manufacturer = $manufacturer;
$this->in_stock = $in_stock;
$this->last_update = $last_update ?? new DateTime();
}
}
use models\Product;
$product = new Product(
null, 'iPhone X', 999.99, 'USD', 64, 'Apple', true, new Date('2017-10-01T00:00:00'));
$product = $session->store($product);
var_dump($product instanceof Product); // true
var_dump(str_starts_with($product->id, 'products/')); // true
$session->saveChanges();
$store = new DocumentStore('url', 'databaseName');
$store->setAuthOptions($authOptions); // use auth options to connect on database
$store->initialize();