2009-10-07 08:07:23 +04:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* 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
|
2012-05-26 16:37:00 +04:00
|
|
|
* and is licensed under the MIT license. For more information, see
|
2009-10-07 08:07:23 +04:00
|
|
|
* <http://www.doctrine-project.org>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Doctrine\ORM\Mapping\Driver;
|
|
|
|
|
2012-12-03 13:36:08 +04:00
|
|
|
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;
|
|
|
|
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
|
|
|
|
use Doctrine\Common\Util\Inflector;
|
2013-06-12 10:00:36 +04:00
|
|
|
use Doctrine\DBAL\Schema\AbstractSchemaManager;
|
|
|
|
use Doctrine\DBAL\Schema\SchemaException;
|
|
|
|
use Doctrine\DBAL\Schema\Table;
|
|
|
|
use Doctrine\DBAL\Schema\Column;
|
|
|
|
use Doctrine\DBAL\Types\Type;
|
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo;
|
2012-12-03 13:36:08 +04:00
|
|
|
use Doctrine\ORM\Mapping\MappingException;
|
2009-10-07 08:07:23 +04:00
|
|
|
|
|
|
|
/**
|
2010-04-10 02:00:36 +04:00
|
|
|
* The DatabaseDriver reverse engineers the mapping metadata from a database.
|
2009-10-07 08:07:23 +04:00
|
|
|
*
|
|
|
|
* @link www.doctrine-project.org
|
|
|
|
* @since 2.0
|
|
|
|
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
|
|
|
* @author Jonathan Wage <jonwage@gmail.com>
|
2010-06-14 01:02:18 +04:00
|
|
|
* @author Benjamin Eberlei <kontakt@beberlei.de>
|
2009-10-07 08:07:23 +04:00
|
|
|
*/
|
2012-01-17 21:09:49 +04:00
|
|
|
class DatabaseDriver implements MappingDriver
|
2009-10-07 08:07:23 +04:00
|
|
|
{
|
2010-06-20 21:34:09 +04:00
|
|
|
/**
|
|
|
|
* @var AbstractSchemaManager
|
|
|
|
*/
|
2009-10-07 08:07:23 +04:00
|
|
|
private $_sm;
|
2010-06-20 21:34:09 +04:00
|
|
|
|
|
|
|
/**
|
2012-12-03 13:36:08 +04:00
|
|
|
* @var array|null
|
2010-06-20 21:34:09 +04:00
|
|
|
*/
|
|
|
|
private $tables = null;
|
|
|
|
|
2012-12-03 13:36:08 +04:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-10-30 14:35:22 +04:00
|
|
|
private $classToTableNames = array();
|
2010-06-21 01:39:21 +04:00
|
|
|
|
2010-06-20 21:34:09 +04:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $manyToManyTables = array();
|
2011-05-20 18:36:43 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $classNamesForTables = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $fieldNamesForColumns = array();
|
|
|
|
|
2011-06-02 23:45:03 +04:00
|
|
|
/**
|
|
|
|
* The namespace for the generated entities.
|
|
|
|
*
|
2012-12-03 13:36:08 +04:00
|
|
|
* @var string|null
|
2011-06-02 23:45:03 +04:00
|
|
|
*/
|
|
|
|
private $namespace;
|
|
|
|
|
2009-10-07 08:07:23 +04:00
|
|
|
/**
|
2012-01-17 20:44:19 +04:00
|
|
|
* @param AbstractSchemaManager $schemaManager
|
2009-10-07 08:07:23 +04:00
|
|
|
*/
|
|
|
|
public function __construct(AbstractSchemaManager $schemaManager)
|
|
|
|
{
|
|
|
|
$this->_sm = $schemaManager;
|
|
|
|
}
|
2010-06-20 21:34:09 +04:00
|
|
|
|
2013-06-12 10:00:36 +04:00
|
|
|
/**
|
|
|
|
* Set the namespace for the generated entities.
|
|
|
|
*
|
|
|
|
* @param string $namespace
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setNamespace($namespace)
|
|
|
|
{
|
|
|
|
$this->namespace = $namespace;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function isTransient($className)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function getAllClassNames()
|
|
|
|
{
|
|
|
|
$this->reverseEngineerMappingFromDatabase();
|
|
|
|
|
|
|
|
return array_keys($this->classToTableNames);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets class name for a table.
|
|
|
|
*
|
|
|
|
* @param string $tableName
|
|
|
|
* @param string $className
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setClassNameForTable($tableName, $className)
|
|
|
|
{
|
|
|
|
$this->classNamesForTables[$tableName] = $className;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets field name for a column on a specific table.
|
|
|
|
*
|
|
|
|
* @param string $tableName
|
|
|
|
* @param string $columnName
|
|
|
|
* @param string $fieldName
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setFieldNameForColumn($tableName, $columnName, $fieldName)
|
|
|
|
{
|
|
|
|
$this->fieldNamesForColumns[$tableName][$columnName] = $fieldName;
|
|
|
|
}
|
|
|
|
|
2011-04-30 13:16:30 +04:00
|
|
|
/**
|
2013-03-11 04:08:58 +04:00
|
|
|
* Sets tables manually instead of relying on the reverse engineering capabilities of SchemaManager.
|
2011-04-30 13:16:30 +04:00
|
|
|
*
|
|
|
|
* @param array $entityTables
|
|
|
|
* @param array $manyToManyTables
|
2012-12-03 13:36:08 +04:00
|
|
|
*
|
2011-04-30 13:16:30 +04:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setTables($entityTables, $manyToManyTables)
|
|
|
|
{
|
|
|
|
$this->tables = $this->manyToManyTables = $this->classToTableNames = array();
|
2013-06-12 10:00:36 +04:00
|
|
|
|
2012-03-24 14:16:32 +04:00
|
|
|
foreach ($entityTables as $table) {
|
2011-05-20 18:53:35 +04:00
|
|
|
$className = $this->getClassNameForTable($table->getName());
|
2013-06-12 10:00:36 +04:00
|
|
|
|
2011-04-30 13:16:30 +04:00
|
|
|
$this->classToTableNames[$className] = $table->getName();
|
|
|
|
$this->tables[$table->getName()] = $table;
|
|
|
|
}
|
2013-06-12 10:00:36 +04:00
|
|
|
|
2012-03-24 14:16:32 +04:00
|
|
|
foreach ($manyToManyTables as $table) {
|
2011-04-30 13:16:30 +04:00
|
|
|
$this->manyToManyTables[$table->getName()] = $table;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-12 10:00:36 +04:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function loadMetadataForClass($className, ClassMetadata $metadata)
|
|
|
|
{
|
|
|
|
$this->reverseEngineerMappingFromDatabase();
|
|
|
|
|
|
|
|
if ( ! isset($this->classToTableNames[$className])) {
|
|
|
|
throw new \InvalidArgumentException("Unknown class " . $className);
|
|
|
|
}
|
|
|
|
|
|
|
|
$tableName = $this->classToTableNames[$className];
|
|
|
|
|
|
|
|
$metadata->name = $className;
|
|
|
|
$metadata->table['name'] = $tableName;
|
|
|
|
|
|
|
|
$this->buildIndexes($metadata);
|
|
|
|
$this->buildFieldMappings($metadata);
|
|
|
|
$this->buildToOneAssociationMappings($metadata);
|
|
|
|
|
|
|
|
foreach ($this->manyToManyTables as $manyTable) {
|
|
|
|
foreach ($manyTable->getForeignKeys() as $foreignKey) {
|
|
|
|
// foreign key maps to the table of the current entity, many to many association probably exists
|
|
|
|
if ( ! (strtolower($tableName) === strtolower($foreignKey->getForeignTableName()))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$myFk = $foreignKey;
|
|
|
|
$otherFk = null;
|
|
|
|
|
|
|
|
foreach ($manyTable->getForeignKeys() as $foreignKey) {
|
|
|
|
if ($foreignKey != $myFk) {
|
|
|
|
$otherFk = $foreignKey;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! $otherFk) {
|
|
|
|
// the definition of this many to many table does not contain
|
|
|
|
// enough foreign key information to continue reverse engineering.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$localColumn = current($myFk->getColumns());
|
|
|
|
|
|
|
|
$associationMapping = array();
|
|
|
|
$associationMapping['fieldName'] = $this->getFieldNameForColumn($manyTable->getName(), current($otherFk->getColumns()), true);
|
|
|
|
$associationMapping['targetEntity'] = $this->getClassNameForTable($otherFk->getForeignTableName());
|
|
|
|
|
|
|
|
if (current($manyTable->getColumns())->getName() == $localColumn) {
|
|
|
|
$associationMapping['inversedBy'] = $this->getFieldNameForColumn($manyTable->getName(), current($myFk->getColumns()), true);
|
|
|
|
$associationMapping['joinTable'] = array(
|
|
|
|
'name' => strtolower($manyTable->getName()),
|
|
|
|
'joinColumns' => array(),
|
|
|
|
'inverseJoinColumns' => array(),
|
|
|
|
);
|
|
|
|
|
|
|
|
$fkCols = $myFk->getForeignColumns();
|
|
|
|
$cols = $myFk->getColumns();
|
|
|
|
|
|
|
|
for ($i = 0; $i < count($cols); $i++) {
|
|
|
|
$associationMapping['joinTable']['joinColumns'][] = array(
|
|
|
|
'name' => $cols[$i],
|
|
|
|
'referencedColumnName' => $fkCols[$i],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$fkCols = $otherFk->getForeignColumns();
|
|
|
|
$cols = $otherFk->getColumns();
|
|
|
|
|
|
|
|
for ($i = 0; $i < count($cols); $i++) {
|
|
|
|
$associationMapping['joinTable']['inverseJoinColumns'][] = array(
|
|
|
|
'name' => $cols[$i],
|
|
|
|
'referencedColumnName' => $fkCols[$i],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$associationMapping['mappedBy'] = $this->getFieldNameForColumn($manyTable->getName(), current($myFk->getColumns()), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
$metadata->mapManyToMany($associationMapping);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-03 13:36:08 +04:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*
|
|
|
|
* @throws \Doctrine\ORM\Mapping\MappingException
|
|
|
|
*/
|
2010-06-20 21:34:09 +04:00
|
|
|
private function reverseEngineerMappingFromDatabase()
|
|
|
|
{
|
|
|
|
if ($this->tables !== null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-06-20 21:07:03 +04:00
|
|
|
$tables = array();
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2010-06-20 21:34:09 +04:00
|
|
|
foreach ($this->_sm->listTableNames() as $tableName) {
|
2010-10-30 14:35:22 +04:00
|
|
|
$tables[$tableName] = $this->_sm->listTableDetails($tableName);
|
2010-06-20 21:34:09 +04:00
|
|
|
}
|
|
|
|
|
2011-04-30 13:16:30 +04:00
|
|
|
$this->tables = $this->manyToManyTables = $this->classToTableNames = array();
|
2013-06-12 10:00:36 +04:00
|
|
|
|
2012-03-24 14:16:32 +04:00
|
|
|
foreach ($tables as $tableName => $table) {
|
2013-06-12 10:00:36 +04:00
|
|
|
$foreignKeys = ($this->_sm->getDatabasePlatform()->supportsForeignKeyConstraints())
|
|
|
|
? $table->getForeignKeys()
|
|
|
|
: array();
|
2010-06-20 21:34:09 +04:00
|
|
|
|
|
|
|
$allForeignKeyColumns = array();
|
2013-06-12 10:00:36 +04:00
|
|
|
|
2012-03-24 14:16:32 +04:00
|
|
|
foreach ($foreignKeys as $foreignKey) {
|
2010-06-20 21:34:09 +04:00
|
|
|
$allForeignKeyColumns = array_merge($allForeignKeyColumns, $foreignKey->getLocalColumns());
|
|
|
|
}
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2012-03-15 00:09:48 +04:00
|
|
|
if ( ! $table->hasPrimaryKey()) {
|
|
|
|
throw new MappingException(
|
|
|
|
"Table " . $table->getName() . " has no primary key. Doctrine does not ".
|
|
|
|
"support reverse engineering from tables that don't have a primary key."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2010-06-20 21:34:09 +04:00
|
|
|
$pkColumns = $table->getPrimaryKey()->getColumns();
|
2013-06-12 10:00:36 +04:00
|
|
|
|
2010-06-20 21:34:09 +04:00
|
|
|
sort($pkColumns);
|
|
|
|
sort($allForeignKeyColumns);
|
|
|
|
|
2011-04-30 13:16:30 +04:00
|
|
|
if ($pkColumns == $allForeignKeyColumns && count($foreignKeys) == 2) {
|
2010-10-30 14:35:22 +04:00
|
|
|
$this->manyToManyTables[$tableName] = $table;
|
2010-06-20 21:34:09 +04:00
|
|
|
} else {
|
2010-10-30 14:35:22 +04:00
|
|
|
// lower-casing is necessary because of Oracle Uppercase Tablenames,
|
|
|
|
// assumption is lower-case + underscore separated.
|
2011-05-20 18:53:35 +04:00
|
|
|
$className = $this->getClassNameForTable($tableName);
|
2013-06-12 10:00:36 +04:00
|
|
|
|
2010-10-30 14:35:22 +04:00
|
|
|
$this->tables[$tableName] = $table;
|
|
|
|
$this->classToTableNames[$className] = $tableName;
|
2010-06-20 21:34:09 +04:00
|
|
|
}
|
2010-06-21 01:39:21 +04:00
|
|
|
}
|
2010-06-20 21:34:09 +04:00
|
|
|
}
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2009-10-07 08:07:23 +04:00
|
|
|
/**
|
2013-06-12 10:00:36 +04:00
|
|
|
* Build indexes from a class metadata.
|
|
|
|
*
|
|
|
|
* @param \Doctrine\ORM\Mapping\ClassMetadataInfo $metadata
|
2009-10-07 08:07:23 +04:00
|
|
|
*/
|
2013-06-12 10:00:36 +04:00
|
|
|
private function buildIndexes(ClassMetadataInfo $metadata)
|
2009-10-07 08:07:23 +04:00
|
|
|
{
|
2013-06-12 10:00:36 +04:00
|
|
|
$tableName = $metadata->table['name'];
|
|
|
|
$indexes = $this->tables[$tableName]->getIndexes();
|
2010-06-20 21:34:09 +04:00
|
|
|
|
2013-06-12 10:00:36 +04:00
|
|
|
foreach($indexes as $index){
|
|
|
|
if ($index->isPrimary()) {
|
|
|
|
continue;
|
|
|
|
}
|
2009-10-07 08:07:23 +04:00
|
|
|
|
2013-06-12 10:00:36 +04:00
|
|
|
$indexName = $index->getName();
|
|
|
|
$indexColumns = $index->getColumns();
|
|
|
|
$constraintType = $index->isUnique()
|
|
|
|
? 'uniqueConstraints'
|
|
|
|
: 'indexes';
|
2009-10-07 08:07:23 +04:00
|
|
|
|
2013-06-12 10:00:36 +04:00
|
|
|
$metadata->table[$constraintType][$indexName]['columns'] = $indexColumns;
|
2010-12-31 01:18:00 +03:00
|
|
|
}
|
2013-06-12 10:00:36 +04:00
|
|
|
}
|
2011-12-20 01:56:19 +04:00
|
|
|
|
2013-06-12 10:00:36 +04:00
|
|
|
/**
|
|
|
|
* Build field mapping from class metadata.
|
|
|
|
*
|
|
|
|
* @param \Doctrine\ORM\Mapping\ClassMetadataInfo $metadata
|
|
|
|
*/
|
|
|
|
private function buildFieldMappings(ClassMetadataInfo $metadata)
|
|
|
|
{
|
|
|
|
$tableName = $metadata->table['name'];
|
|
|
|
$columns = $this->tables[$tableName]->getColumns();
|
|
|
|
$primaryKeys = $this->getTablePrimaryKeys($this->tables[$tableName]);
|
|
|
|
$foreignKeys = $this->getTableForeignKeys($this->tables[$tableName]);
|
|
|
|
$allForeignKeys = array();
|
2009-10-07 08:07:23 +04:00
|
|
|
|
2012-03-24 14:16:32 +04:00
|
|
|
foreach ($foreignKeys as $foreignKey) {
|
2013-06-12 10:00:36 +04:00
|
|
|
$allForeignKeys = array_merge($allForeignKeys, $foreignKey->getLocalColumns());
|
2010-06-13 21:36:49 +04:00
|
|
|
}
|
|
|
|
|
2013-06-12 10:00:36 +04:00
|
|
|
$ids = array();
|
2009-10-07 08:07:23 +04:00
|
|
|
$fieldMappings = array();
|
2012-06-09 17:17:45 +04:00
|
|
|
|
2013-06-12 10:00:36 +04:00
|
|
|
foreach ($columns as $column) {
|
|
|
|
if (in_array($column->getName(), $allForeignKeys)) {
|
2010-06-14 01:02:18 +04:00
|
|
|
continue;
|
2009-10-07 08:07:23 +04:00
|
|
|
}
|
2012-06-09 17:17:45 +04:00
|
|
|
|
2013-06-12 10:00:36 +04:00
|
|
|
$fieldMapping = $this->buildFieldMapping($tableName, $column);
|
2009-12-05 00:40:03 +03:00
|
|
|
|
2013-06-12 10:00:36 +04:00
|
|
|
if ($primaryKeys && in_array($column->getName(), $primaryKeys)) {
|
|
|
|
$fieldMapping['id'] = true;
|
2009-10-07 08:07:23 +04:00
|
|
|
$ids[] = $fieldMapping;
|
|
|
|
}
|
|
|
|
|
2013-06-12 10:00:36 +04:00
|
|
|
$fieldMappings[] = $fieldMapping;
|
|
|
|
}
|
2009-10-07 08:07:23 +04:00
|
|
|
|
2013-06-12 10:00:36 +04:00
|
|
|
// We need to check for the columns here, because we might have associations as id as well.
|
|
|
|
if ($ids && count($primaryKeys) == 1) {
|
|
|
|
$metadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO);
|
2009-10-07 08:07:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($fieldMappings as $fieldMapping) {
|
|
|
|
$metadata->mapField($fieldMapping);
|
|
|
|
}
|
2013-06-12 10:00:36 +04:00
|
|
|
}
|
2009-10-07 08:07:23 +04:00
|
|
|
|
2013-06-12 10:00:36 +04:00
|
|
|
/**
|
|
|
|
* Build field mapping from a schema column definition
|
|
|
|
*
|
|
|
|
* @param string $tableName
|
|
|
|
* @param \Doctrine\DBAL\Schema\Column $column
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function buildFieldMapping($tableName, Column $column)
|
|
|
|
{
|
|
|
|
$fieldMapping = array(
|
|
|
|
'fieldName' => $this->getFieldNameForColumn($tableName, $column->getName(), false),
|
|
|
|
'columnName' => $column->getName(),
|
|
|
|
'type' => strtolower((string) $column->getType()),
|
|
|
|
'nullable' => ( ! $column->getNotNull()),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Type specific elements
|
|
|
|
switch ($fieldMapping['type']) {
|
|
|
|
case Type::TARRAY:
|
|
|
|
case Type::BLOB:
|
|
|
|
case Type::GUID:
|
|
|
|
case Type::JSON_ARRAY:
|
|
|
|
case Type::OBJECT:
|
|
|
|
case Type::SIMPLE_ARRAY:
|
|
|
|
case Type::STRING:
|
|
|
|
case Type::TEXT:
|
|
|
|
$fieldMapping['length'] = $column->getLength();
|
|
|
|
$fieldMapping['fixed'] = $column->getFixed();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Type::DECIMAL:
|
|
|
|
case Type::FLOAT:
|
|
|
|
$fieldMapping['precision'] = $column->getPrecision();
|
|
|
|
$fieldMapping['scale'] = $column->getScale();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Type::INTEGER:
|
|
|
|
case Type::BIGINT:
|
|
|
|
case Type::SMALLINT:
|
|
|
|
$fieldMapping['unsigned'] = $column->getUnsigned();
|
|
|
|
break;
|
|
|
|
}
|
2011-04-30 13:16:30 +04:00
|
|
|
|
2013-06-12 10:00:36 +04:00
|
|
|
// Comment
|
|
|
|
if (($comment = $column->getComment()) !== null) {
|
|
|
|
$fieldMapping['comment'] = $comment;
|
|
|
|
}
|
2010-06-20 21:34:09 +04:00
|
|
|
|
2013-06-12 10:00:36 +04:00
|
|
|
// Default
|
|
|
|
if (($default = $column->getDefault()) !== null) {
|
|
|
|
$fieldMapping['default'] = $default;
|
2010-06-20 21:34:09 +04:00
|
|
|
}
|
|
|
|
|
2013-06-12 10:00:36 +04:00
|
|
|
return $fieldMapping;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build to one (one to one, many to one) association mapping from class metadata.
|
|
|
|
*
|
|
|
|
* @param \Doctrine\ORM\Mapping\ClassMetadataInfo $metadata
|
|
|
|
*/
|
|
|
|
private function buildToOneAssociationMappings(ClassMetadataInfo $metadata)
|
|
|
|
{
|
|
|
|
$tableName = $metadata->table['name'];
|
|
|
|
$primaryKeys = $this->getTablePrimaryKeys($this->tables[$tableName]);
|
|
|
|
$foreignKeys = $this->getTableForeignKeys($this->tables[$tableName]);
|
2009-12-05 00:40:03 +03:00
|
|
|
|
2013-06-12 10:00:36 +04:00
|
|
|
foreach ($foreignKeys as $foreignKey) {
|
|
|
|
$foreignTableName = $foreignKey->getForeignTableName();
|
|
|
|
$fkColumns = $foreignKey->getColumns();
|
|
|
|
$fkForeignColumns = $foreignKey->getForeignColumns();
|
|
|
|
$localColumn = current($fkColumns);
|
|
|
|
$associationMapping = array(
|
|
|
|
'fieldName' => $this->getFieldNameForColumn($tableName, $localColumn, true),
|
|
|
|
'targetEntity' => $this->getClassNameForTable($foreignTableName),
|
|
|
|
);
|
2012-03-05 12:29:06 +04:00
|
|
|
|
2012-10-05 22:00:12 +04:00
|
|
|
if (isset($metadata->fieldMappings[$associationMapping['fieldName']])) {
|
2013-06-12 10:00:36 +04:00
|
|
|
$associationMapping['fieldName'] .= '2'; // "foo" => "foo2"
|
2012-10-05 22:00:12 +04:00
|
|
|
}
|
|
|
|
|
2013-06-12 10:00:36 +04:00
|
|
|
if ($primaryKeys && in_array($localColumn, $primaryKeys)) {
|
2012-02-13 17:05:28 +04:00
|
|
|
$associationMapping['id'] = true;
|
|
|
|
}
|
2010-02-14 01:28:33 +03:00
|
|
|
|
2013-06-12 10:00:36 +04:00
|
|
|
for ($i = 0; $i < count($fkColumns); $i++) {
|
2010-02-14 01:28:33 +03:00
|
|
|
$associationMapping['joinColumns'][] = array(
|
2013-06-12 10:00:36 +04:00
|
|
|
'name' => $fkColumns[$i],
|
|
|
|
'referencedColumnName' => $fkForeignColumns[$i],
|
2010-02-14 01:28:33 +03:00
|
|
|
);
|
|
|
|
}
|
2012-06-09 17:17:45 +04:00
|
|
|
|
2013-06-12 10:00:36 +04:00
|
|
|
// Here we need to check if $fkColumns are the same as $primaryKeys
|
|
|
|
if ( ! array_diff($fkColumns, $primaryKeys)) {
|
2012-02-13 13:35:55 +04:00
|
|
|
$metadata->mapOneToOne($associationMapping);
|
2012-03-05 12:29:06 +04:00
|
|
|
} else {
|
2012-02-13 13:35:55 +04:00
|
|
|
$metadata->mapManyToOne($associationMapping);
|
2012-02-13 17:05:28 +04:00
|
|
|
}
|
2009-10-07 08:07:23 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-12 10:00:36 +04:00
|
|
|
* Retreive schema table definition foreign keys.
|
2011-05-20 18:36:43 +04:00
|
|
|
*
|
2013-06-12 10:00:36 +04:00
|
|
|
* @param \Doctrine\DBAL\Schema\Table $table
|
2012-12-03 13:36:08 +04:00
|
|
|
*
|
2013-06-12 10:00:36 +04:00
|
|
|
* @return array
|
2011-05-20 18:36:43 +04:00
|
|
|
*/
|
2013-06-12 10:00:36 +04:00
|
|
|
private function getTableForeignKeys(Table $table)
|
2011-05-20 18:36:43 +04:00
|
|
|
{
|
2013-06-12 10:00:36 +04:00
|
|
|
return ($this->_sm->getDatabasePlatform()->supportsForeignKeyConstraints())
|
|
|
|
? $table->getForeignKeys()
|
|
|
|
: array();
|
2011-05-20 18:36:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-12 10:00:36 +04:00
|
|
|
* Retreive schema table definition primary keys.
|
2011-05-20 18:36:43 +04:00
|
|
|
*
|
2013-06-12 10:00:36 +04:00
|
|
|
* @param \Doctrine\DBAL\Schema\Table $table
|
2012-12-03 13:36:08 +04:00
|
|
|
*
|
2013-06-12 10:00:36 +04:00
|
|
|
* @return array
|
2011-05-20 18:36:43 +04:00
|
|
|
*/
|
2013-06-12 10:00:36 +04:00
|
|
|
private function getTablePrimaryKeys(Table $table)
|
2011-05-20 18:36:43 +04:00
|
|
|
{
|
2013-06-12 10:00:36 +04:00
|
|
|
try {
|
|
|
|
return $table->getPrimaryKey()->getColumns();
|
|
|
|
} catch(SchemaException $e) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
return array();
|
2011-05-20 18:36:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-12-03 13:36:08 +04:00
|
|
|
* Returns the mapped class name for a table if it exists. Otherwise return "classified" version.
|
2011-05-20 18:36:43 +04:00
|
|
|
*
|
|
|
|
* @param string $tableName
|
2012-12-03 13:36:08 +04:00
|
|
|
*
|
2011-05-20 18:36:43 +04:00
|
|
|
* @return string
|
|
|
|
*/
|
2011-05-20 18:53:35 +04:00
|
|
|
private function getClassNameForTable($tableName)
|
2011-05-20 18:36:43 +04:00
|
|
|
{
|
|
|
|
if (isset($this->classNamesForTables[$tableName])) {
|
2011-06-02 23:45:03 +04:00
|
|
|
return $this->namespace . $this->classNamesForTables[$tableName];
|
2011-05-20 18:36:43 +04:00
|
|
|
}
|
|
|
|
|
2011-06-02 23:45:03 +04:00
|
|
|
return $this->namespace . Inflector::classify(strtolower($tableName));
|
2011-05-20 18:36:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the mapped field name for a column, if it exists. Otherwise return camelized version.
|
|
|
|
*
|
2012-12-03 13:36:08 +04:00
|
|
|
* @param string $tableName
|
|
|
|
* @param string $columnName
|
2011-05-20 18:36:43 +04:00
|
|
|
* @param boolean $fk Whether the column is a foreignkey or not.
|
2012-12-03 13:36:08 +04:00
|
|
|
*
|
2011-05-20 18:36:43 +04:00
|
|
|
* @return string
|
|
|
|
*/
|
2011-05-20 18:53:35 +04:00
|
|
|
private function getFieldNameForColumn($tableName, $columnName, $fk = false)
|
2011-05-20 18:36:43 +04:00
|
|
|
{
|
|
|
|
if (isset($this->fieldNamesForColumns[$tableName]) && isset($this->fieldNamesForColumns[$tableName][$columnName])) {
|
|
|
|
return $this->fieldNamesForColumns[$tableName][$columnName];
|
|
|
|
}
|
|
|
|
|
|
|
|
$columnName = strtolower($columnName);
|
|
|
|
|
|
|
|
// Replace _id if it is a foreignkey column
|
|
|
|
if ($fk) {
|
|
|
|
$columnName = str_replace('_id', '', $columnName);
|
|
|
|
}
|
|
|
|
return Inflector::camelize($columnName);
|
|
|
|
}
|
|
|
|
}
|