respect/structural

Fluent NoSQL Toolkit

dev-master / 0.1.x-dev 2012-05-05 19:11 UTC

This package is auto-updated.

Last update: 2024-03-24 03:33:52 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

Build Status Scrutinizer Code Quality Code Coverage StyleCI

The Near-zero Part

// bootstrap.php
require_once __DIR__ . '/vendor/autoload.php';

use Respect\Structural\Mapper;
use Respect\Structural\Driver\MongoDb\Style as MongoDbStyle;
use Respect\Structural\Driver\MongoDb\Driver as MongoDbDriver;

$driver = MongoDbDriver::factory('respect');

$mapper = new Mapper($driver);
$mapper->setStyle(new MongoDbStyle());

Persisting

$author = new \stdClass();
$author->firstName = 'Antonio';
$mapper->authors->persist($author);
$mapper->flush();

echo "'{$author->firstName}' was created with id({$author->_id})".PHP_EOL;

Updating

$author->lastName = 'Spinelli';
$mapper->authors->persist($author);
$mapper->flush();

echo "last name was updated to '{$author->lastName}' from id({$author->_id})".PHP_EOL;

Fetching

$authors = $mapper->authors->fetchAll();

echo "Fetching all authors:" . PHP_EOL;
foreach ($authors as $index => $author) {
    echo "{$index} {$author->firstName} {$author->lastName}" . PHP_EOL;
}

Condition

// find author by ID
$foundAuthor = $mapper->authors[(string)$author->_id]->fetch();
echo "find by id('{$author->_id}') {$foundAuthor->firstName} {$foundAuthor->lastName}".PHP_EOL;

Removing

$mapper->authors->remove($author);
$mapper->flush();

$author = $mapper->authors(['lastName' => 'Spinelli'])->fetch();
echo ($author ? "'Spinelli' was found" : "'Spinelli' removed.");