2009-10-07 08:07:23 +04:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* $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>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Doctrine\ORM\Mapping\Driver;
|
|
|
|
|
2010-02-03 00:17:00 +03:00
|
|
|
use Doctrine\Common\Cache\ArrayCache,
|
2009-10-07 08:07:23 +04:00
|
|
|
Doctrine\Common\Annotations\AnnotationReader,
|
|
|
|
Doctrine\DBAL\Schema\AbstractSchemaManager,
|
|
|
|
Doctrine\ORM\Mapping\ClassMetadataInfo,
|
|
|
|
Doctrine\ORM\Mapping\MappingException,
|
|
|
|
Doctrine\Common\Util\Inflector;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The DatabaseDriver reverse engineers the mapping metadata from a database
|
|
|
|
*
|
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @link www.doctrine-project.org
|
|
|
|
* @since 2.0
|
|
|
|
* @version $Revision$
|
|
|
|
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
|
|
|
* @author Jonathan Wage <jonwage@gmail.com>
|
|
|
|
*/
|
|
|
|
class DatabaseDriver implements Driver
|
|
|
|
{
|
|
|
|
/** The SchemaManager. */
|
|
|
|
private $_sm;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes a new AnnotationDriver that uses the given AnnotationReader for reading
|
|
|
|
* docblock annotations.
|
|
|
|
*
|
|
|
|
* @param AnnotationReader $reader The AnnotationReader to use.
|
|
|
|
*/
|
|
|
|
public function __construct(AbstractSchemaManager $schemaManager)
|
|
|
|
{
|
|
|
|
$this->_sm = $schemaManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function loadMetadataForClass($className, ClassMetadataInfo $metadata)
|
|
|
|
{
|
|
|
|
$tableName = $className;
|
|
|
|
$className = Inflector::classify($tableName);
|
|
|
|
|
|
|
|
$metadata->name = $className;
|
|
|
|
$metadata->primaryTable['name'] = $tableName;
|
|
|
|
|
|
|
|
$columns = $this->_sm->listTableColumns($tableName);
|
2009-12-05 00:40:03 +03:00
|
|
|
|
|
|
|
if($this->_sm->getDatabasePlatform()->supportsForeignKeyConstraints()) {
|
2009-10-08 22:54:19 +04:00
|
|
|
$foreignKeys = $this->_sm->listTableForeignKeys($tableName);
|
2009-12-05 00:40:03 +03:00
|
|
|
} else {
|
2009-10-08 22:54:19 +04:00
|
|
|
$foreignKeys = array();
|
|
|
|
}
|
2009-10-07 08:07:23 +04:00
|
|
|
|
2009-12-05 00:40:03 +03:00
|
|
|
$indexes = $this->_sm->listTableIndexes($tableName);
|
|
|
|
|
2009-10-07 08:07:23 +04:00
|
|
|
$ids = array();
|
|
|
|
$fieldMappings = array();
|
|
|
|
foreach ($columns as $column) {
|
|
|
|
// Skip columns that are foreign keys
|
|
|
|
foreach ($foreignKeys as $foreignKey) {
|
2009-12-05 12:39:11 +03:00
|
|
|
if (in_array(strtolower($column->getName()), array_map('strtolower', $foreignKey->getColumns()))) {
|
2009-10-07 08:07:23 +04:00
|
|
|
continue(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$fieldMapping = array();
|
2009-12-11 02:55:47 +03:00
|
|
|
if (isset($indexes['primary']) && in_array($column->getName(), $indexes['primary']->getColumns())) {
|
2009-10-07 08:07:23 +04:00
|
|
|
$fieldMapping['id'] = true;
|
|
|
|
}
|
|
|
|
|
2010-02-07 15:36:30 +03:00
|
|
|
$fieldMapping['fieldName'] = Inflector::camelize(strtolower($column->getName()));
|
2009-12-05 00:40:03 +03:00
|
|
|
$fieldMapping['columnName'] = $column->getName();
|
|
|
|
$fieldMapping['type'] = strtolower((string) $column->getType());
|
|
|
|
|
|
|
|
if ($column->getType() instanceof \Doctrine\DBAL\Types\StringType) {
|
|
|
|
$fieldMapping['length'] = $column->getLength();
|
|
|
|
$fieldMapping['fixed'] = $column->getFixed();
|
|
|
|
} else if ($column->getType() instanceof \Doctrine\DBAL\Types\IntegerType) {
|
|
|
|
$fieldMapping['unsigned'] = $column->getUnsigned();
|
|
|
|
}
|
|
|
|
$fieldMapping['notnull'] = $column->getNotNull();
|
2009-10-07 08:07:23 +04:00
|
|
|
|
|
|
|
if (isset($fieldMapping['id'])) {
|
|
|
|
$ids[] = $fieldMapping;
|
|
|
|
} else {
|
|
|
|
$fieldMappings[] = $fieldMapping;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($ids) {
|
2009-10-08 22:54:19 +04:00
|
|
|
if (count($ids) == 1) {
|
|
|
|
$metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO);
|
|
|
|
}
|
2009-10-07 08:07:23 +04:00
|
|
|
|
|
|
|
foreach ($ids as $id) {
|
|
|
|
$metadata->mapField($id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($fieldMappings as $fieldMapping) {
|
|
|
|
$metadata->mapField($fieldMapping);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($foreignKeys as $foreignKey) {
|
2009-12-05 00:40:03 +03:00
|
|
|
if (count($foreignKey->getColumns()) != 1) {
|
|
|
|
throw new MappingException(
|
|
|
|
"Cannot generate mapping for table '".$tableName."' with foreign keys with multiple local columns."
|
|
|
|
);
|
|
|
|
}
|
2009-12-07 16:04:54 +03:00
|
|
|
$cols = $foreignKey->getColumns();
|
|
|
|
$localColumn = current($cols);
|
2009-12-05 00:40:03 +03:00
|
|
|
|
2009-12-07 16:04:54 +03:00
|
|
|
$fkCols = $foreignKey->getForeignColumns();
|
|
|
|
if (count($fkCols) != 1) {
|
2009-12-05 00:40:03 +03:00
|
|
|
throw new MappingException(
|
|
|
|
"Cannot generate mapping for table '".$tableName."' with foreign keys with multiple foreign columns."
|
|
|
|
);
|
|
|
|
}
|
2009-12-07 16:04:54 +03:00
|
|
|
$foreignColumn = current($fkCols);
|
2009-12-05 00:40:03 +03:00
|
|
|
|
2009-10-07 08:07:23 +04:00
|
|
|
$associationMapping = array();
|
2009-12-05 12:39:11 +03:00
|
|
|
$associationMapping['fieldName'] = Inflector::camelize(str_ireplace('_id', '', $localColumn));
|
2009-12-05 00:40:03 +03:00
|
|
|
$associationMapping['columnName'] = $localColumn;
|
|
|
|
$associationMapping['targetEntity'] = Inflector::classify($foreignKey->getForeignTableName());
|
2009-10-07 08:07:23 +04:00
|
|
|
$associationMapping['joinColumns'][] = array(
|
2009-12-05 00:40:03 +03:00
|
|
|
'name' => $localColumn,
|
|
|
|
'referencedColumnName' => $foreignColumn
|
2009-10-07 08:07:23 +04:00
|
|
|
);
|
|
|
|
|
|
|
|
$metadata->mapManyToOne($associationMapping);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-01-28 15:46:12 +03:00
|
|
|
* {@inheritdoc}
|
2009-10-07 08:07:23 +04:00
|
|
|
*/
|
|
|
|
public function isTransient($className)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-12-16 00:06:32 +03:00
|
|
|
* {@inheritDoc}
|
2009-10-07 08:07:23 +04:00
|
|
|
*/
|
2009-12-16 00:06:32 +03:00
|
|
|
public function getAllClassNames()
|
2009-10-07 08:07:23 +04:00
|
|
|
{
|
2010-01-28 15:46:12 +03:00
|
|
|
$classes = array();
|
|
|
|
|
2009-10-07 08:07:23 +04:00
|
|
|
foreach ($this->_sm->listTables() as $table) {
|
2010-01-28 15:46:12 +03:00
|
|
|
$classes[] = $table->getName(); // TODO: Why is this not correct? Inflector::classify($table->getName());
|
2009-10-07 08:07:23 +04:00
|
|
|
}
|
|
|
|
|
2010-01-28 15:46:12 +03:00
|
|
|
return $classes;
|
2009-10-07 08:07:23 +04:00
|
|
|
}
|
|
|
|
}
|