PHP code example of jamesmoss / flywheel

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

    

jamesmoss / flywheel example snippets


$config = new \JamesMoss\Flywheel\Config('path/to/writable/directory');

// The repository is responsible for storing, updating and deleting documents.
$repo = new \JamesMoss\Flywheel\Repository('posts', $config);

// Storing a new document
$post = new \JamesMoss\Flywheel\Document(array(
    'title'     => 'An introduction to Flywheel',
    'dateAdded' => new \DateTime('2013-10-10'),
    'body'      => 'A lightweight, flat-file, document database for PHP...',
    'wordCount' => 7,
    'author'    => 'James',
    'published' => true,
    'translations' => array(
    	'de' => 'Eine Einführung in Flywheel',
    	'it' => 'Una introduzione a Flywheel',
    ),
));

echo $post->title; // An introduction to Flywheel
echo $post->wordCount; // 7

$id = $repo->store($post);

// A unique ID is automatically generated for you if you don't specify your own when storing.
// The generated ID consists of upper/lowercase letters and numbers so is URL safe.
echo $id; // Czk6SPu4X
echo $post->getId(); // Czk6SPu4X

// If you set your own then it cannot contain the following characters: / ? * : ; { } \ or newline
$post->setId('a-review-of-2013');

// Retrieving documents
$posts = $repo->query()
    ->where('dateAdded', '>', new \DateTime('2013-11-18'))
    ->andWhere('published', '==', true)
    ->orderBy('wordCount DESC')
    ->limit(10, 5)
    ->execute();

echo count($posts); // 5 the number of documents returned in this result
echo $posts->total() // 33 the number of documents if no limit was applied. Useful for pagination.

foreach($posts as $post) {
    echo $post->title;
}

// Pull one document out of the repo by ID
$post = $repo->findById('a-quick-guide-to-flywheel');

// Updating documents
$post->title = 'How to update documents';

// Updates the document (only if it already exists)
$repo->update($post);


// Deleting documents - you can pass a document or it's ID.
$repo->delete($post);
// or you can do the following
$repo->delete('Czk6SPu4X');


// Find posts with more than 100 words written by James
$posts = $repo->query()
    ->where('wordCount', '>', 100)
    ->andWhere('author', '==', 'James')
    ->execute();

// The special __id field name can be used to query by the document's ID.
// Find all posts where the ID is either 1 or 7 or 8.
$posts = $repo->query()
    ->where('__id', '==', 1)
    ->orWhere('__id', '==', 7)
    ->orWhere('__id', '==', 8)
    ->execute();

// A neater, alternative way of doing the above query
$posts = $repo->query()
    ->where('__id', 'IN', array(1, 7, 8))
    ->execute();

// You can query by sub keys within a document too. The following finds all
// documents which have a German translation
$posts = $repo->query()
    ->where('translations.de', '!=', false)
    ->execute();

$posts = $repo->query()
    ->where('wordCount', '>', 100)
    ->andWhere(function($query) {
    	$query->where('author', '==', 'Hugo')
    	$query->orWhere('dateAdded', '>', new \DateTime('2014-05-04'))
    })
    ->execute();

// A simple example
$posts = $repo->query()
    ->orderBy('wordCount ASC')
    ->execute();

// You can use sub keys too
$posts = $repo->query()
    ->orderBy('translations.de DESC')
    ->execute();
    
// It's possible to sort on multiple fields by passing in an array to orderBy
$posts = $repo->query()
    ->orderBy(['name ASC', 'age DESC'])
    ->execute();

// Use the special __id field name to sort by the document's ID
$posts = $repo->query()
    ->orderBy('__id')
    ->execute();

// Get the last 5 blog posts
$posts = $repo->query()
	->where('published', '==', 'true')
    ->orderBy('dateAdded DESC')
    ->limit(5)
    ->execute();

 // Get 25 blog posts offset by 100 from the start.
 $posts = $repo->query()
 	->where('published', '==', 'true')
    ->orderBy('dateAdded DESC')
    ->limit(25, 100)
    ->execute();

$config = new Config('/path/to/writable/directory', array(
    'formatter' => new \JamesMoss\Flywheel\Formatter\YAML,
))