From c67c8eac87a9066be95c781b40a141b5cef0be02 Mon Sep 17 00:00:00 2001 From: jwage Date: Tue, 17 Feb 2009 08:01:34 +0000 Subject: [PATCH] [2.0] Initial entry of YAML schema meta data driver and sandbox. A few other misc. fixes as well. --- UPGRADE_TO_2_0 | 4 +- lib/Doctrine/DBAL/DriverManager.php | 6 +- lib/Doctrine/DBAL/Types/IntegerType.php | 10 +- lib/Doctrine/ORM/EntityManager.php | 28 +++- .../ORM/Mapping/Driver/YamlDriver.php | 129 ++++++++++++++++-- tools/sandbox/config.php | 18 +++ tools/sandbox/index.php | 2 + 7 files changed, 178 insertions(+), 19 deletions(-) create mode 100644 tools/sandbox/config.php create mode 100644 tools/sandbox/index.php diff --git a/UPGRADE_TO_2_0 b/UPGRADE_TO_2_0 index 134c9dade..42be4398c 100644 --- a/UPGRADE_TO_2_0 +++ b/UPGRADE_TO_2_0 @@ -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); diff --git a/lib/Doctrine/DBAL/DriverManager.php b/lib/Doctrine/DBAL/DriverManager.php index 1df4fc9e7..8ca285f5c 100644 --- a/lib/Doctrine/DBAL/DriverManager.php +++ b/lib/Doctrine/DBAL/DriverManager.php @@ -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']); } } } diff --git a/lib/Doctrine/DBAL/Types/IntegerType.php b/lib/Doctrine/DBAL/Types/IntegerType.php index 2d3350c02..8b9a01061 100644 --- a/lib/Doctrine/DBAL/Types/IntegerType.php +++ b/lib/Doctrine/DBAL/Types/IntegerType.php @@ -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; } -} - +} \ No newline at end of file diff --git a/lib/Doctrine/ORM/EntityManager.php b/lib/Doctrine/ORM/EntityManager.php index b370307e0..562ce0b53 100644 --- a/lib/Doctrine/ORM/EntityManager.php +++ b/lib/Doctrine/ORM/EntityManager.php @@ -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. * diff --git a/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php b/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php index 9623373b1..bf6566585 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php @@ -1,16 +1,127 @@ - + * @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; + } } \ No newline at end of file diff --git a/tools/sandbox/config.php b/tools/sandbox/config.php new file mode 100644 index 000000000..32eb48c14 --- /dev/null +++ b/tools/sandbox/config.php @@ -0,0 +1,18 @@ +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); \ No newline at end of file diff --git a/tools/sandbox/index.php b/tools/sandbox/index.php new file mode 100644 index 000000000..a6486a99f --- /dev/null +++ b/tools/sandbox/index.php @@ -0,0 +1,2 @@ +