2009-02-17 11:01:34 +03:00
|
|
|
<?php
|
2009-05-05 21:20:55 +04:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
|
|
|
* and is licensed under the LGPL. For more information, see
|
|
|
|
* <http://www.doctrine-project.org>.
|
|
|
|
*/
|
2009-02-17 11:01:34 +03:00
|
|
|
|
|
|
|
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';
|
|
|
|
}
|
2008-02-04 00:29:57 +03:00
|
|
|
|
|
|
|
/**
|
2009-05-05 21:20:55 +04:00
|
|
|
* The YamlDriver reads the mapping metadata from yaml schema files.
|
2008-02-04 00:29:57 +03:00
|
|
|
*
|
2009-02-17 11:01:34 +03:00
|
|
|
* @author Jonathan H. Wage <jonwage@gmail.com>
|
|
|
|
* @since 2.0
|
2008-02-04 00:29:57 +03:00
|
|
|
*/
|
2009-06-06 01:40:47 +04:00
|
|
|
class YamlDriver extends AbstractFileDriver
|
2008-02-04 00:29:57 +03:00
|
|
|
{
|
2009-06-06 01:40:47 +04:00
|
|
|
protected $_fileExtension = '.dcm.yml';
|
2009-02-17 11:01:34 +03:00
|
|
|
|
|
|
|
public function loadMetadataForClass($className, ClassMetadata $metadata)
|
|
|
|
{
|
2009-08-25 01:05:55 +04:00
|
|
|
$class = $metadata->getReflectionClass();
|
|
|
|
|
2009-06-06 01:40:47 +04:00
|
|
|
$element = $this->getElement($className);
|
2009-02-17 11:01:34 +03:00
|
|
|
|
2009-06-06 01:40:47 +04:00
|
|
|
if ($element['type'] == 'entity') {
|
2009-08-25 01:05:55 +04:00
|
|
|
$metadata->setCustomRepositoryClass(
|
|
|
|
isset($element['repositoryClass']) ? $xmlRoot['repositoryClass'] : null
|
|
|
|
);
|
|
|
|
} else if ($element['type'] == 'mappedSuperclass') {
|
|
|
|
$metadata->isMappedSuperclass = true;
|
|
|
|
} else {
|
2009-08-27 02:03:39 +04:00
|
|
|
throw DoctrineException::classIsNotAValidEntityOrMapperSuperClass($className);
|
2009-08-25 01:05:55 +04:00
|
|
|
}
|
2009-02-17 11:01:34 +03:00
|
|
|
|
2009-08-25 01:05:55 +04:00
|
|
|
// Evaluate root level properties
|
|
|
|
if (isset($element['table'])) {
|
|
|
|
$metadata->primaryTable['name'] = $element['table'];
|
|
|
|
}
|
|
|
|
if (isset($element['schema'])) {
|
|
|
|
$metadata->primaryTable['schema'] = $element['schema'];
|
|
|
|
}
|
|
|
|
if (isset($element['inheritanceType'])) {
|
|
|
|
$metadata->setInheritanceType($element['inheritanceType']);
|
|
|
|
}
|
2009-02-17 11:01:34 +03:00
|
|
|
|
2009-08-25 01:05:55 +04:00
|
|
|
// Evaluate discriminatorColumn
|
|
|
|
if (isset($element['discriminatorColumn'])) {
|
|
|
|
$discrColumn = $element['discriminatorColumn'];
|
|
|
|
$metadata->setDiscriminatorColumn(array(
|
|
|
|
'name' => $discrColumn['name'],
|
|
|
|
'type' => $discrColumn['type'],
|
|
|
|
'length' => $discrColumn['length']
|
|
|
|
));
|
|
|
|
}
|
2009-08-24 22:33:59 +04:00
|
|
|
|
2009-08-25 01:05:55 +04:00
|
|
|
// Evaluate discriminatorMap
|
|
|
|
if (isset($element['discriminatorMap'])) {
|
|
|
|
$metadata->setDiscriminatorMap($element['discriminatorMap']);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Evaluate changeTrackingPolicy
|
|
|
|
if (isset($element['changeTrackingPolicy'])) {
|
|
|
|
$metadata->setChangeTrackingPolicy($element['changeTrackingPolicy']);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Evaluate indexes
|
|
|
|
if (isset($element['indexes'])) {
|
|
|
|
foreach ($element['indexes'] as $index) {
|
|
|
|
$metadata->primaryTable['indexes'][$index['name']] = array('fields' =>
|
|
|
|
explode(',', $index['columns']));
|
2009-08-24 22:33:59 +04:00
|
|
|
}
|
2009-08-25 01:05:55 +04:00
|
|
|
}
|
2009-08-24 22:33:59 +04:00
|
|
|
|
2009-08-25 01:05:55 +04:00
|
|
|
// Evaluate uniqueConstraints
|
|
|
|
if (isset($element['uniqueConstraints'])) {
|
|
|
|
foreach ($element['uniqueConstraints'] as $unique) {
|
|
|
|
$metadata->primaryTable['uniqueConstraints'][] = explode(',', $unique['columns']);
|
2009-06-06 01:40:47 +04:00
|
|
|
}
|
2009-08-25 01:05:55 +04:00
|
|
|
}
|
2009-02-17 11:01:34 +03:00
|
|
|
|
2009-08-25 01:05:55 +04:00
|
|
|
// Evaluate fields
|
|
|
|
if (isset($element['fields'])) {
|
|
|
|
foreach ($element['fields'] as $name => $fieldMapping) {
|
2009-06-06 01:40:47 +04:00
|
|
|
$mapping = array(
|
|
|
|
'fieldName' => $name,
|
2009-08-25 01:05:55 +04:00
|
|
|
'type' => $fieldMapping['type']
|
2009-06-06 01:40:47 +04:00
|
|
|
);
|
2009-08-25 01:05:55 +04:00
|
|
|
if (isset($fieldMapping['column'])) {
|
|
|
|
$mapping['columnName'] = $fieldMapping['column'];
|
2009-06-06 01:40:47 +04:00
|
|
|
}
|
2009-08-25 01:05:55 +04:00
|
|
|
if (isset($fieldMapping['length'])) {
|
|
|
|
$mapping['length'] = $fieldMapping['length'];
|
|
|
|
}
|
|
|
|
if (isset($fieldMapping['precision'])) {
|
|
|
|
$mapping['precision'] = $fieldMapping['precision'];
|
|
|
|
}
|
|
|
|
if (isset($fieldMapping['scale'])) {
|
|
|
|
$mapping['scale'] = $fieldMapping['scale'];
|
2009-06-06 01:40:47 +04:00
|
|
|
}
|
2009-08-25 01:05:55 +04:00
|
|
|
if (isset($fieldMapping['version']) && $fieldMapping['version']) {
|
|
|
|
$metadata->setVersionMapping($mapping);
|
|
|
|
}
|
|
|
|
$metadata->mapField($mapping);
|
2009-06-06 01:40:47 +04:00
|
|
|
}
|
2009-08-25 01:05:55 +04:00
|
|
|
}
|
2009-02-17 11:01:34 +03:00
|
|
|
|
2009-08-25 01:05:55 +04:00
|
|
|
// Evaluate identifier settings
|
|
|
|
foreach ($element['id'] as $name => $idElement) {
|
|
|
|
$mapping = array(
|
|
|
|
'id' => true,
|
|
|
|
'fieldName' => $name,
|
|
|
|
'type' => $idElement['type']
|
|
|
|
);
|
|
|
|
if (isset($idElement['column'])) {
|
|
|
|
$mapping['columnName'] = $idElement['column'];
|
2009-02-17 11:01:34 +03:00
|
|
|
}
|
2009-08-25 01:05:55 +04:00
|
|
|
$metadata->mapField($mapping);
|
2009-02-17 11:01:34 +03:00
|
|
|
|
2009-08-25 01:05:55 +04:00
|
|
|
if (isset($idElement['generator'])) {
|
|
|
|
$metadata->setIdGeneratorType(constant('Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_'
|
|
|
|
. $idElement['generator']['strategy']));
|
2009-06-06 01:40:47 +04:00
|
|
|
}
|
2009-08-25 01:05:55 +04:00
|
|
|
}
|
2009-02-17 11:01:34 +03:00
|
|
|
|
2009-08-25 01:05:55 +04:00
|
|
|
// Evaluate oneToOne relationships
|
|
|
|
if (isset($element['oneToOne'])) {
|
|
|
|
foreach ($element['oneToOne'] as $name => $oneToOneElement) {
|
|
|
|
$mapping = array(
|
|
|
|
'fieldName' => $name,
|
|
|
|
'targetEntity' => $oneToOneElement['targetEntity']
|
|
|
|
);
|
|
|
|
if (isset($oneToOneElement['mappedBy'])) {
|
|
|
|
$mapping['mappedBy'] = $oneToOneElement['mappedBy'];
|
|
|
|
} else {
|
2009-06-06 01:40:47 +04:00
|
|
|
$joinColumns = array();
|
2009-08-25 01:05:55 +04:00
|
|
|
if (isset($oneToOneElement['joinColumn'])) {
|
|
|
|
$joinColumns[] = $this->_getJoinColumnMapping($oneToOneElement['joinColumn']);
|
|
|
|
} else if (isset($oneToOneElement['joinColumns'])) {
|
|
|
|
foreach ($oneToOneElement['joinColumns'] as $name => $joinColumnElement) {
|
2009-06-06 01:40:47 +04:00
|
|
|
if (!isset($joinColumnElement['name'])) {
|
|
|
|
$joinColumnElement['name'] = $name;
|
|
|
|
}
|
|
|
|
$joinColumns[] = $this->_getJoinColumnMapping($joinColumnElement);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw MappingException::invalidMapping($mapping['fieldName']);
|
|
|
|
}
|
|
|
|
$mapping['joinColumns'] = $joinColumns;
|
2009-08-25 01:05:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($oneToOneElement['cascade'])) {
|
|
|
|
$mapping['cascade'] = $this->_getCascadeMappings($oneToOneElement['cascade']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$metadata->mapOneToOne($mapping);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Evaluate oneToMany relationships
|
|
|
|
if (isset($element['oneToMany'])) {
|
|
|
|
foreach ($element['oneToMany'] as $name => $oneToManyElement) {
|
|
|
|
$mapping = array(
|
|
|
|
'fieldName' => $name,
|
|
|
|
'targetEntity' => $oneToManyElement['targetEntity'],
|
|
|
|
'mappedBy' => $oneToManyElement['mappedBy']
|
|
|
|
);
|
|
|
|
if (isset($oneToManyElement['cascade'])) {
|
|
|
|
$mapping['cascade'] = $this->_getCascadeMappings($oneToManyElement['cascade']);
|
|
|
|
}
|
|
|
|
$metadata->mapOneToMany($mapping);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Evaluate manyToOne relationships
|
|
|
|
if (isset($element['manyToOne'])) {
|
|
|
|
foreach ($element['manyToOne'] as $name => $manyToOneElement) {
|
|
|
|
$mapping = array(
|
|
|
|
'fieldName' => $name,
|
|
|
|
'targetEntity' => $manyToOneElement['targetEntity']
|
|
|
|
);
|
|
|
|
$joinColumns = array();
|
|
|
|
if (isset($manyToOneElement['joinColumn'])) {
|
|
|
|
$joinColumns[] = $this->_getJoinColumnMapping($manyToOneElement['joinColumn']);
|
|
|
|
} else if (isset($manyToOneElement['joinColumns'])) {
|
|
|
|
foreach ($manyToOneElement['joinColumns'] as $name => $joinColumnElement) {
|
|
|
|
if (!isset($joinColumnElement['name'])) {
|
|
|
|
$joinColumnElement['name'] = $name;
|
|
|
|
}
|
|
|
|
$joinColumns[] = $this->_getJoinColumnMapping($joinColumnElement);
|
2009-06-06 01:40:47 +04:00
|
|
|
}
|
2009-08-25 01:05:55 +04:00
|
|
|
} else {
|
|
|
|
throw MappingException::invalidMapping($mapping['fieldName']);
|
|
|
|
}
|
|
|
|
$mapping['joinColumns'] = $joinColumns;
|
|
|
|
if (isset($manyToOneElement['cascade'])) {
|
|
|
|
$mapping['cascade'] = $this->_getCascadeMappings($manyToOneElement['cascade']);
|
2009-06-06 01:40:47 +04:00
|
|
|
}
|
2009-08-25 01:05:55 +04:00
|
|
|
$metadata->mapManyToOne($mapping);
|
2009-06-06 01:40:47 +04:00
|
|
|
}
|
2009-08-25 01:05:55 +04:00
|
|
|
}
|
2009-02-17 11:01:34 +03:00
|
|
|
|
2009-08-25 01:05:55 +04:00
|
|
|
// Evaluate manyToMany relationships
|
|
|
|
if (isset($element['manyToMany'])) {
|
|
|
|
foreach ($element['manyToMany'] as $name => $manyToManyElement) {
|
|
|
|
$mapping = array(
|
|
|
|
'fieldName' => $name,
|
|
|
|
'targetEntity' => $manyToManyElement['targetEntity']
|
|
|
|
);
|
|
|
|
|
|
|
|
if (isset($manyToManyElement['mappedBy'])) {
|
|
|
|
$mapping['mappedBy'] = $manyToManyElement['mappedBy'];
|
|
|
|
} else if (isset($manyToManyElement['joinTable'])) {
|
|
|
|
$joinTableElement = $manyToManyElement['joinTable'];
|
|
|
|
$joinTable = array(
|
|
|
|
'name' => $joinTableElement['name']
|
2009-06-06 01:40:47 +04:00
|
|
|
);
|
2009-08-25 01:05:55 +04:00
|
|
|
if (isset($joinTableElement['schema'])) {
|
|
|
|
$joinTable['schema'] = $joinTableElement['schema'];
|
|
|
|
}
|
|
|
|
foreach ($joinTableElement['joinColumns'] as $name => $joinColumnElement) {
|
|
|
|
if (!isset($joinColumnElement['name'])) {
|
|
|
|
$joinColumnElement['name'] = $name;
|
2009-02-17 11:01:34 +03:00
|
|
|
}
|
2009-08-25 01:05:55 +04:00
|
|
|
$joinTable['joinColumns'][] = $this->_getJoinColumnMapping($joinColumnElement);
|
2009-06-06 01:40:47 +04:00
|
|
|
}
|
2009-08-25 01:05:55 +04:00
|
|
|
foreach ($joinTableElement['inverseJoinColumns'] as $name => $joinColumnElement) {
|
|
|
|
if (!isset($joinColumnElement['name'])) {
|
|
|
|
$joinColumnElement['name'] = $name;
|
|
|
|
}
|
|
|
|
$joinTable['inverseJoinColumns'][] = $this->_getJoinColumnMapping($joinColumnElement);
|
2009-06-06 01:40:47 +04:00
|
|
|
}
|
2009-08-25 01:05:55 +04:00
|
|
|
$mapping['joinTable'] = $joinTable;
|
|
|
|
} else {
|
|
|
|
throw MappingException::invalidMapping($mapping['fieldName']);
|
2009-06-06 01:40:47 +04:00
|
|
|
}
|
2009-08-25 01:05:55 +04:00
|
|
|
|
|
|
|
if (isset($manyToManyElement['cascade'])) {
|
|
|
|
$mapping['cascade'] = $this->_getCascadeMappings($manyToManyElement['cascade']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$metadata->mapManyToMany($mapping);
|
2009-02-17 11:01:34 +03:00
|
|
|
}
|
2009-08-25 01:05:55 +04:00
|
|
|
}
|
2009-06-06 01:40:47 +04:00
|
|
|
|
2009-08-25 13:14:08 +04:00
|
|
|
// Evaluate lifeCycleCallbacks
|
|
|
|
if (isset($element['lifecycleCallbacks'])) {
|
|
|
|
foreach ($element['lifecycleCallbacks'] as $method => $type) {
|
2009-08-25 01:05:55 +04:00
|
|
|
$method = $class->getMethod($method);
|
|
|
|
if ($method->isPublic()) {
|
2009-08-25 13:14:08 +04:00
|
|
|
$metadata->addLifecycleCallback($method->getName(), constant('\Doctrine\ORM\Events::' . $type));
|
2009-08-25 01:05:55 +04:00
|
|
|
}
|
|
|
|
}
|
2009-02-17 11:01:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-06 01:40:47 +04:00
|
|
|
/**
|
|
|
|
* Constructs a joinColumn mapping array based on the information
|
2009-06-06 01:58:05 +04:00
|
|
|
* found in the given join column element.
|
2009-06-06 01:40:47 +04:00
|
|
|
*
|
2009-06-06 01:58:05 +04:00
|
|
|
* @param $joinColumnElement The array join column element
|
2009-06-06 01:40:47 +04:00
|
|
|
* @return array The mapping array.
|
|
|
|
*/
|
|
|
|
private function _getJoinColumnMapping($joinColumnElement)
|
2008-02-04 00:29:57 +03:00
|
|
|
{
|
2009-06-06 01:40:47 +04:00
|
|
|
$joinColumn = array(
|
|
|
|
'name' => $joinColumnElement['name'],
|
|
|
|
'referencedColumnName' => $joinColumnElement['referencedColumnName']
|
|
|
|
);
|
|
|
|
if (isset($joinColumnElement['unique'])) {
|
|
|
|
$joinColumn['unique'] = (bool) $joinColumnElement['unique'];
|
|
|
|
}
|
|
|
|
if (isset($joinColumnElement['nullable'])) {
|
|
|
|
$joinColumn['nullable'] = (bool) $joinColumnElement['nullable'];
|
|
|
|
}
|
|
|
|
if (isset($joinColumnElement['onDelete'])) {
|
|
|
|
$joinColumn['onDelete'] = $joinColumnElement['onDelete'];
|
|
|
|
}
|
|
|
|
if (isset($joinColumnElement['onUpdate'])) {
|
|
|
|
$joinColumn['onUpdate'] = $joinColumnElement['onUpdate'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $joinColumn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gathers a list of cascade options found in the given cascade element.
|
|
|
|
*
|
|
|
|
* @param $cascadeElement The cascade element.
|
|
|
|
* @return array The list of cascade options.
|
|
|
|
*/
|
|
|
|
private function _getCascadeMappings($cascadeElement)
|
|
|
|
{
|
|
|
|
$cascades = array();
|
2009-07-25 20:33:29 +04:00
|
|
|
if (isset($cascadeElement['cascadePersist'])) {
|
|
|
|
$cascades[] = 'persist';
|
2009-02-17 11:01:34 +03:00
|
|
|
}
|
2009-07-25 20:33:29 +04:00
|
|
|
if (isset($cascadeElement['cascadeRemove'])) {
|
|
|
|
$cascades[] = 'remove';
|
2009-06-06 01:40:47 +04:00
|
|
|
}
|
|
|
|
if (isset($cascadeElement['cascadeMerge'])) {
|
|
|
|
$cascades[] = 'merge';
|
|
|
|
}
|
|
|
|
if (isset($cascadeElement['cascadeRefresh'])) {
|
|
|
|
$cascades[] = 'refresh';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $cascades;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads a mapping file with the given name and returns a map
|
|
|
|
* from class/entity names to their corresponding elements.
|
|
|
|
*
|
|
|
|
* @param string $file The mapping file to load.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function _loadMappingFile($file)
|
|
|
|
{
|
|
|
|
return \sfYaml::load($file);
|
2009-02-17 11:01:34 +03:00
|
|
|
}
|
2008-02-04 00:29:57 +03:00
|
|
|
}
|