Skip to content
This repository has been archived by the owner on May 28, 2021. It is now read-only.

Commit

Permalink
Simplify examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
nielssp committed Jun 18, 2016
1 parent 4f6f4a2 commit 9636644
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 43 deletions.
38 changes: 21 additions & 17 deletions examples/example1.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
<?php
// Example: Using dynamic table definitions

use Jivoo\Store\Document;
use Jivoo\Data\Database\DatabaseDefinitionBuilder;
use Jivoo\Data\Database\DatabaseSchema;
use Jivoo\Data\Database\Loader;
use Jivoo\Log\Logger;
use Jivoo\Data\DefinitionBuilder;
use Jivoo\Log\CallbackHandler;
use Jivoo\Log\Logger;

// Include Jivoo by using composer:
require '../vendor/autoload.php';

// Initialize database loader with connection settings for "default" database:
$loader = new Loader(new Document(array(
'default' => array(
'driver' => 'PdoMysql',
'server' => 'localhost',
'username' => 'jivoo',
'database' => 'jivoo'
)
)));
// Initialize database loader
$loader = new Loader();

// Log database queries to output
$logger = new Logger();
Expand All @@ -28,20 +23,29 @@
}));
$loader->setLogger($logger);

$definition = new Jivoo\Data\Database\DatabaseDefinitionBuilder();
$definition->addDefinition('User', Jivoo\Data\DefinitionBuilder::auto(['username', 'created']));
// Initialize definition for User-table
$definition = new DatabaseDefinitionBuilder();
$definition->addDefinition('User', DefinitionBuilder::auto(['username', 'created']));

// Connect to "default":
$db = new \Jivoo\Data\Database\DatabaseSchema($loader->connect('default', $definition));
// Connect to database
$schema = new DatabaseSchema($loader->connect(
[
'driver' => 'PdoMysql',
'server' => 'localhost',
'username' => 'jivoo',
'database' => 'jivoo'
],
$definition
));


echo '<pre>';

// Get data for root user:
print_r($db->User->where('username = %s', 'root')->first()->getData());
print_r($schema->User->where('username = %s', 'root')->first()->getData());

// List names of users created after 2015-01-01
$users = $db->User
$users = $schema->User
->where('created > %d', '2015-01-01') // Converts date using strtotime()
->orderBy('created');

Expand Down
41 changes: 21 additions & 20 deletions examples/example2.php
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
<?php
use Jivoo\Store\Document;
use Jivoo\Data\DataType;

use Jivoo\Data\Database\DatabaseDefinitionBuilder;
use Jivoo\Data\Database\SchemaBuilder;
use Jivoo\Data\Database\DatabaseSchema;
use Jivoo\Data\Database\Loader;
use Jivoo\Log\Logger;
use Jivoo\Data\DataType;
use Jivoo\Data\DefinitionBuilder;
use Jivoo\Log\CallbackHandler;
use Jivoo\Log\Logger;

// Include Jivoo by using composer:
require '../vendor/autoload.php';

// Initialize database loader with connection settings for "default" database:
$loader = new Loader(new Document(array(
'default' => array(
'driver' => 'PdoMysql',
'server' => 'localhost',
'username' => 'jivoo',
'database' => 'jivoo',
'tablePrefix' => 'test_'
)
)));
// Initialize database loader
$loader = new Loader();

class User {
public static function getDefinition() {
$def = new \Jivoo\Data\DefinitionBuilder();
$def = new DefinitionBuilder();
$def->addAutoIncrementId(); // Autoincrementing INT id
$def->username = DataType::string(255); // Username VARCHAR(255)
$def->password = DataType::string(255); // Password VARCHAR(255)
Expand All @@ -47,8 +40,16 @@ public static function getDefinition() {
'User' => User::getDefinition()
]);

// Connect to "default":
$db = $loader->connect('default', $definition);
// Connect to database
$db = $loader->connect(
[
'driver' => 'PdoMysql',
'server' => 'localhost',
'username' => 'jivoo',
'database' => 'jivoo'
],
$definition
);

echo '<pre>';

Expand All @@ -60,15 +61,15 @@ public static function getDefinition() {
// Create table
$db->User->create();

$schema = new Jivoo\Data\Database\DatabaseSchema($db);
$schema = new DatabaseSchema($db);

// Insert a user (array style)
$schema->User->insert(array(
$schema->User->insert([
'username' => 'root',
'password' => 'secret',
'created' => time(),
'updated' => time()
));
]);

// Insert a user (active record style)
$user = $schema->User->create();
Expand Down
26 changes: 20 additions & 6 deletions src/Database/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@ class Loader implements LoggerAware
/**
* Construct database loader.
*/
public function __construct(Document $config)
public function __construct(Document $config = null)
{
$this->logger = new NullLogger();
if (! isset($config)) {
$config = new Document();
}
$this->config = $config;
$this->drivers = dirname(__FILE__) . '/Drivers';
}
Expand Down Expand Up @@ -208,7 +211,7 @@ public function readDefinition($namespace, $dir)
/**
* Make a database connection.
*
* @param string $name
* @param string|Document|array $name
* Name of database connection.
* @param DatabaseDefinition $definition
* Database definition (collecton of table definitions).
Expand All @@ -221,10 +224,19 @@ public function readDefinition($namespace, $dir)
*/
public function connect($name, DatabaseDefinition $definition = null)
{
if (! isset($this->config[$name])) {
throw new ConfigurationException('Database "' . $name . '" not configured');
if (is_string($name)) {
if (! isset($this->config[$name])) {
throw new ConfigurationException('Database "' . $name . '" not configured');
}
$config = $this->config->getSubset($name);
} else if (is_array($name)) {
$config = new Document($name);
unset($name);
} else {
Assume::isInstanceOf($name, 'Jivoo\Store\Document');
$config = $name;
unset($name);
}
$config = $this->config->getSubset($name);
$driver = $config->get('driver', null);
if (! isset($driver)) {
throw new ConfigurationException('Database driver not set');
Expand All @@ -247,7 +259,9 @@ public function connect($name, DatabaseDefinition $definition = null)
}
$object = new $class($definition, $config);
$object->setLogger($this->logger);
$this->connections[$name] = new DatabaseSchema($object);
if (isset($name)) {
$this->connections[$name] = new DatabaseSchema($object);
}
return $object;
} catch (ConnectionException $exception) {
throw new ConnectionException(
Expand Down

0 comments on commit 9636644

Please sign in to comment.