[2.0] Initial entry of YAML schema meta data driver and sandbox. A few other misc. fixes as well.
This commit is contained in:
parent
4dc5a4baf5
commit
c67c8eac87
@ -52,8 +52,8 @@ Then, when trying to load the class Doctrine\ORM\EntityManager, for example the
|
||||
$config->setMetadataCacheImpl(new \Doctrine\ORM\Cache\ArrayCache);
|
||||
$eventManager = new \Doctrine\Common\EventManager();
|
||||
$connectionOptions = array(
|
||||
'user' => 'john',
|
||||
'password' => 'wayne'
|
||||
'driver' => 'pdo_sqlite',
|
||||
'path' => 'database.sqlite'
|
||||
);
|
||||
$em = \Doctrine\ORM\EntityManager::create($connectionOptions, 'doctrine', $config, $eventManager);
|
||||
|
||||
|
@ -107,7 +107,7 @@ final class DriverManager
|
||||
|
||||
// check for existing pdo object
|
||||
if (isset($params['pdo']) && ! $params['pdo'] instanceof PDO) {
|
||||
throw DBALException::invalidPDOInstance();
|
||||
throw Exceptions\DBALException::invalidPDOInstance();
|
||||
} else if (isset($params['pdo'])) {
|
||||
$params['driver'] = $params['pdo']->getAttribute(PDO::ATTR_DRIVER_NAME);
|
||||
} else {
|
||||
@ -140,14 +140,14 @@ final class DriverManager
|
||||
|
||||
// driver
|
||||
if ( ! isset($params['driver']) && ! isset($params['driverClass'])) {
|
||||
throw DBALException::driverRequired();
|
||||
throw Exceptions\DBALException::driverRequired();
|
||||
}
|
||||
|
||||
// check validity of parameters
|
||||
|
||||
// driver
|
||||
if ( isset($params['driver']) && ! isset(self::$_driverMap[$params['driver']])) {
|
||||
throw DBALException::unknownDriver($params['driver']);
|
||||
throw Exceptions\DBALException::unknownDriver($params['driver']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -9,7 +9,10 @@ namespace Doctrine\DBAL\Types;
|
||||
class IntegerType extends Type
|
||||
{
|
||||
|
||||
public function getName() { return "Integer"; }
|
||||
public function getName()
|
||||
{
|
||||
return "Integer";
|
||||
}
|
||||
|
||||
public function getSqlDeclaration(array $fieldDeclaration, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
|
||||
{
|
||||
@ -18,7 +21,6 @@ class IntegerType extends Type
|
||||
|
||||
public function convertToPHPValue($value)
|
||||
{
|
||||
return (int)$value;
|
||||
return (int) $value;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -448,7 +448,33 @@ class EntityManager
|
||||
{
|
||||
//...
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
public function toArray($entity, $deep = false)
|
||||
{
|
||||
$array = array();
|
||||
foreach ($entity as $key => $value) {
|
||||
if ($deep && is_object($value)) {
|
||||
$array[$key] = $this->toArray($value, $deep);
|
||||
} else if ( ! is_object($value)) {
|
||||
$array[$key] = $value;
|
||||
}
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
|
||||
public function fromArray($entity, array $array, $deep = false)
|
||||
{
|
||||
foreach ($array as $key => $value) {
|
||||
if ($deep && is_array($value)) {
|
||||
$entity->$key = $this->fromArray($entity, $value, $deep);
|
||||
} else if ( ! is_array($value)) {
|
||||
$entity->$key = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Gets the repository for an entity class.
|
||||
*
|
||||
|
@ -1,16 +1,127 @@
|
||||
<?php
|
||||
<?php
|
||||
|
||||
namespace Doctrine\ORM\Mapping\Driver;
|
||||
|
||||
use Doctrine\ORM\Mapping\ClassMetadata;
|
||||
|
||||
if ( ! class_exists('sfYaml', false)) {
|
||||
require_once __DIR__ . '/../../../../vendor/sfYaml/sfYaml.class.php';
|
||||
require_once __DIR__ . '/../../../../vendor/sfYaml/sfYamlDumper.class.php';
|
||||
require_once __DIR__ . '/../../../../vendor/sfYaml/sfYamlInline.class.php';
|
||||
require_once __DIR__ . '/../../../../vendor/sfYaml/sfYamlParser.class.php';
|
||||
}
|
||||
|
||||
/**
|
||||
* The yaml driver loads metadata informations about classes from .yml files.
|
||||
* The YamlDriver reads the mapping metadata from yaml schema files
|
||||
*
|
||||
* @author Jonathan H. Wage <jonwage@gmail.com>
|
||||
* @since 2.0
|
||||
*/
|
||||
class Doctrine_ORM_Mapping_Driver_YamlDriver
|
||||
class YamlDriver
|
||||
{
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function loadMetadataForClass($className, Doctrine_ORM_Mapping_ClassMetadata $metadata)
|
||||
protected
|
||||
$_paths = array(),
|
||||
$_entities = array();
|
||||
|
||||
public function __construct($paths)
|
||||
{
|
||||
throw new Doctrine_Exception("YAML driver not yet implemented.");
|
||||
}
|
||||
$this->_paths = $paths;
|
||||
$this->_entities = $this->_loadYaml($this->_paths);
|
||||
}
|
||||
|
||||
public function getEntities()
|
||||
{
|
||||
return $this->_entities;
|
||||
}
|
||||
|
||||
public function loadMetadataForClass($className, ClassMetadata $metadata)
|
||||
{
|
||||
$entity = $this->_entities[$className];
|
||||
|
||||
if (isset($entity['repositoryClass']) && $entity['repositoryClass']) {
|
||||
$metadata->setCustomRepositoryClass($entity['repositoryClass']);
|
||||
}
|
||||
|
||||
if (isset($entity['table'])) {
|
||||
$metadata->setPrimaryTable($entity['table']);
|
||||
}
|
||||
|
||||
if (isset($entity['inheritanceType']) && $entity['inheritanceType']) {
|
||||
$metadata->setInheritanceType($entity['inheritanceType']);
|
||||
}
|
||||
|
||||
if (isset($entity['discriminatorColumn'])) {
|
||||
$metadata->setDiscriminatorColumn($entity['discriminatorColumn']);
|
||||
}
|
||||
|
||||
if (isset($entity['discriminatorMap']) && $entity['discriminatorMap']) {
|
||||
$metadata->setDiscriminatorMap((array) $entity['discriminatorMap']);
|
||||
}
|
||||
|
||||
if (isset($entity['subClasses']) && $entity['subClasses']) {
|
||||
$metadata->setSubclasses((array) $entity['subClasses']);
|
||||
}
|
||||
|
||||
$relationTypes = array('OneToOne', 'OneToMany', 'ManyToOne', 'ManyToMany');
|
||||
|
||||
foreach ($entity['properties'] as $name => $property) {
|
||||
$mapping = array();
|
||||
$mapping['fieldName'] = $name;
|
||||
|
||||
$joinColumns = array();
|
||||
if (isset($property['joinColumn']) && $property['joinColumn']) {
|
||||
$joinColumns[] = $property['joinColumn'];
|
||||
} else if (isset($property['joinColumns']) && $property['joinColumns']) {
|
||||
$joinColumns = $property['joinColumns'];
|
||||
}
|
||||
|
||||
$type = $property['type'];
|
||||
|
||||
$mapping = array_merge($mapping, $property);
|
||||
|
||||
if (in_array($type, $relationTypes)) {
|
||||
unset($property['type']);
|
||||
|
||||
switch ($type)
|
||||
{
|
||||
case 'ManyToOne':
|
||||
case 'OneToOne':
|
||||
$mapping['joinColumns'] = $joinColumns;
|
||||
break;
|
||||
case 'ManyToMany':
|
||||
$joinTable = array();
|
||||
if (isset($property['joinTable'])) {
|
||||
$joinTable = $property['joinTable'];
|
||||
}
|
||||
$mapping['joinTable'] = $joinTable;
|
||||
break;
|
||||
case 'OneToMany':
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
$func = 'map' . $type;
|
||||
$metadata->$func($mapping);
|
||||
} else {
|
||||
$metadata->mapField($mapping);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
protected function _loadYaml($paths)
|
||||
{
|
||||
$array = array();
|
||||
foreach ((array) $paths as $path) {
|
||||
if (is_dir($path)) {
|
||||
$files = glob($path . '/*.yml');
|
||||
foreach ($files as $file) {
|
||||
$array = array_merge($array, \sfYaml::load($file));
|
||||
}
|
||||
} else if (is_file($path)) {
|
||||
$array = array_merge($array, \sfYaml::load($path));
|
||||
}
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
}
|
18
tools/sandbox/config.php
Normal file
18
tools/sandbox/config.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
require '../../lib/Doctrine/Common/ClassLoader.php';
|
||||
|
||||
$classLoader = new \Doctrine\Common\ClassLoader();
|
||||
$classLoader->register();
|
||||
|
||||
$classLoader->setBasePath('Doctrine', realpath(__DIR__ . '/../../lib'));
|
||||
$classLoader->setBasePath('Entities', __DIR__);
|
||||
|
||||
$config = new \Doctrine\ORM\Configuration();
|
||||
$config->setMetadataCacheImpl(new \Doctrine\ORM\Cache\ArrayCache);
|
||||
$config->setMetadataDriverImpl(new \Doctrine\ORM\Mapping\Driver\YamlDriver(__DIR__ . '/schema'));
|
||||
$eventManager = new \Doctrine\Common\EventManager();
|
||||
$connectionOptions = array(
|
||||
'driver' => 'pdo_sqlite',
|
||||
'path' => 'database.sqlite'
|
||||
);
|
||||
$em = \Doctrine\ORM\EntityManager::create($connectionOptions, 'doctrine', $config, $eventManager);
|
2
tools/sandbox/index.php
Normal file
2
tools/sandbox/index.php
Normal file
@ -0,0 +1,2 @@
|
||||
<?php
|
||||
require 'config.php';
|
Loading…
Reference in New Issue
Block a user