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

Commit

Permalink
Fix example 1.
Browse files Browse the repository at this point in the history
  • Loading branch information
nielssp committed May 25, 2016
1 parent 746a065 commit b2f164c
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 17 deletions.
6 changes: 5 additions & 1 deletion examples/example1.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@
}));
$loader->setLogger($logger);

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

// Connect to "default":
$db = new \Jivoo\Data\Database\DatabaseSchema($loader->connect('default'));
$db = new \Jivoo\Data\Database\DatabaseSchema($loader->connect('default', $definition));


echo '<pre>';

Expand Down
2 changes: 1 addition & 1 deletion src/Database/DatabaseDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function getTables();
*
* @param string $table
* Table name.
* @return \Jivoo\Data\Definition Table definition.
* @return \Jivoo\Data\Definition|null Table definition if defined.
*/
public function getDefinition($table);
}
14 changes: 1 addition & 13 deletions src/Database/DatabaseDefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ class DatabaseDefinitionBuilder implements DatabaseDefinition
* @var string[] List of table names.
*/
private $tables = array();

/**
* @var boolean
*/
private $dynamic = false;

/**
* Construct database definition.
Expand All @@ -36,13 +31,11 @@ class DatabaseDefinitionBuilder implements DatabaseDefinition
* table names and definitions or another instance of {@see DatabaseDefinition}.
* @param boolean $dynamic
*/
public function __construct($definitions = array(), $dynamic = false)
public function __construct($definitions = array())
{
$this->dynamic = $dynamic;
if ($definitions instanceof DatabaseDefinitionBuilder) {
$this->tables = $definitions->tables;
$this->definitions = $definitions->definitions;
$this->dynamic = $definitions->dynamic;
} elseif ($definitions instanceof DatabaseDefinition) {
foreach ($definitions->getTables() as $table) {
$this->addDefinition($table, $definitions->getDefinition($table));
Expand All @@ -69,12 +62,7 @@ public function getDefinition($table)
{
if (isset($this->definitions[$table])) {
return $this->definitions[$table];
} elseif ($this->dynamic) {
$this->addDefinition($table, new \Jivoo\Data\DefinitionBuilder());
return $this->definitions[$table];
}
echo $table;
exit;
return null;
}

Expand Down
6 changes: 5 additions & 1 deletion src/Database/DatabaseSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,14 @@ public function __construct(Database $database)
public function __get($model)
{
if (!isset($this->models[$model])) {
$definition = $this->definition->getDefinition($model);
if (! isset($definition)) {
throw new \Jivoo\Data\UndefinedModelException('Undefined model: ' . $model);
}
$this->models[$model] = new \Jivoo\Data\SimpleModel(
$model,
$this->database->$model,
$this->definition->getDefinition($model)
$definition
);
}
return $this->models[$model];
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public function connect($name, DatabaseDefinition $definition = null)
$class = 'Jivoo\Data\Database\Drivers\\' . $driver . '\\' . $driver . 'Database';
Assume::isSubclassOf($class, 'Jivoo\Data\Database\LoadableDatabase');
if (! isset($definition)) {
$definition = new DatabaseDefinitionBuilder([], true);
$definition = new DatabaseDefinitionBuilder([]);
}
$object = new $class($definition, $config);
$object->setLogger($this->logger);
Expand Down
15 changes: 15 additions & 0 deletions src/DefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,19 @@ public function isUnique($key)
}
return false;
}

/**
* Create a definition from the provided field names.
*
* @param string[] $fields Field names.
* @return DefinitionBuilder
*/
public static function auto(array $fields)
{
$definition = new DefinitionBuilder();
foreach ($fields as $field) {
$definition->$field = DataType::text(true);
}
return $definition;
}
}
1 change: 1 addition & 0 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface Schema
* @param string $model
* Model name
* @return Modle Model.
* @throws UndefinedModelException If model is undefined.
*/
public function __get($model);

Expand Down
13 changes: 13 additions & 0 deletions src/UndefinedModelException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
// Jivoo
// Copyright (c) 2015 Niels Sonnich Poulsen (http://nielssp.dk)
// Licensed under the MIT license.
// See the LICENSE file or http://opensource.org/licenses/MIT for more information.
namespace Jivoo\Data;

/**
* Thrown when the requested model is not defined in a schema.
*/
class UndefinedModelException extends \DomainException implements \Jivoo\Exception
{
}

0 comments on commit b2f164c

Please sign in to comment.