2007-05-20 22:40:53 +04:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* $Id: Table.php 1397 2007-05-19 19:54:15Z zYne $
|
|
|
|
*
|
|
|
|
* 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.phpdoctrine.com>.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Doctrine_Relation_Parser
|
|
|
|
*
|
|
|
|
* @package Doctrine
|
2007-10-04 01:43:22 +04:00
|
|
|
* @subpackage Relation
|
|
|
|
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
2007-05-20 22:40:53 +04:00
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @version $Revision: 1397 $
|
|
|
|
* @link www.phpdoctrine.com
|
|
|
|
* @since 1.0
|
|
|
|
*/
|
|
|
|
class Doctrine_Relation_Parser
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var Doctrine_Table $_table the table object this parser belongs to
|
|
|
|
*/
|
|
|
|
protected $_table;
|
|
|
|
/**
|
|
|
|
* @var array $_relations an array containing all the Doctrine_Relation objects for this table
|
|
|
|
*/
|
2007-05-20 23:28:21 +04:00
|
|
|
protected $_relations = array();
|
2007-05-20 22:40:53 +04:00
|
|
|
/**
|
2007-05-20 23:28:21 +04:00
|
|
|
* @var array $_pending relations waiting for parsing
|
2007-05-20 22:40:53 +04:00
|
|
|
*/
|
2007-05-20 23:28:21 +04:00
|
|
|
protected $_pending = array();
|
2007-05-20 22:40:53 +04:00
|
|
|
/**
|
|
|
|
* constructor
|
|
|
|
*
|
|
|
|
* @param Doctrine_Table $table the table object this parser belongs to
|
|
|
|
*/
|
|
|
|
public function __construct(Doctrine_Table $table)
|
|
|
|
{
|
|
|
|
$this->_table = $table;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* getTable
|
|
|
|
*
|
|
|
|
* @return Doctrine_Table the table object this parser belongs to
|
|
|
|
*/
|
|
|
|
public function getTable()
|
|
|
|
{
|
2007-05-20 23:28:21 +04:00
|
|
|
return $this->_table;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* getPendingRelation
|
|
|
|
*
|
|
|
|
* @return array an array defining a pending relation
|
|
|
|
*/
|
|
|
|
public function getPendingRelation($name)
|
|
|
|
{
|
2007-09-03 18:57:18 +04:00
|
|
|
if ( ! isset($this->_pending[$name])) {
|
2007-05-20 23:28:21 +04:00
|
|
|
throw new Doctrine_Relation_Exception('Unknown pending relation ' . $name);
|
2007-09-03 18:57:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->_pending[$name];
|
2007-05-20 22:40:53 +04:00
|
|
|
}
|
2007-07-13 20:22:49 +04:00
|
|
|
|
|
|
|
public function hasRelation($name)
|
|
|
|
{
|
|
|
|
if ( ! isset($this->_pending[$name]) && ! isset($this->_relations[$name])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2007-05-20 22:40:53 +04:00
|
|
|
/**
|
|
|
|
* binds a relation
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param string $field
|
|
|
|
* @return void
|
|
|
|
*/
|
2007-05-20 23:28:21 +04:00
|
|
|
public function bind($name, $options = array())
|
2007-05-20 22:40:53 +04:00
|
|
|
{
|
|
|
|
if (isset($this->relations[$name])) {
|
|
|
|
unset($this->relations[$name]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$lower = strtolower($name);
|
|
|
|
|
2007-05-20 23:28:21 +04:00
|
|
|
if ($this->_table->hasColumn($lower)) {
|
|
|
|
throw new Doctrine_Relation_Exception("Couldn't bind relation. Column with name " . $lower . ' already exists!');
|
2007-05-20 22:40:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$e = explode(' as ', $name);
|
|
|
|
$name = $e[0];
|
2007-05-23 00:11:56 +04:00
|
|
|
$alias = isset($e[1]) ? $e[1] : $name;
|
2007-05-20 22:40:53 +04:00
|
|
|
|
2007-05-21 00:49:42 +04:00
|
|
|
if ( ! isset($options['type'])) {
|
|
|
|
throw new Doctrine_Relation_Exception('Relation type not set.');
|
|
|
|
}
|
|
|
|
|
2007-05-20 23:28:21 +04:00
|
|
|
$this->_pending[$alias] = array_merge($options, array('class' => $name, 'alias' => $alias));
|
2007-09-18 02:19:17 +04:00
|
|
|
/**
|
2007-06-14 22:15:33 +04:00
|
|
|
$m = Doctrine_Manager::getInstance();
|
2007-09-18 02:19:17 +04:00
|
|
|
|
2007-06-14 22:15:33 +04:00
|
|
|
if (isset($options['onDelete'])) {
|
|
|
|
$m->addDeleteAction($name, $this->_table->getComponentName(), $options['onDelete']);
|
|
|
|
}
|
|
|
|
if (isset($options['onUpdate'])) {
|
|
|
|
$m->addUpdateAction($name, $this->_table->getComponentName(), $options['onUpdate']);
|
|
|
|
}
|
2007-09-18 02:19:17 +04:00
|
|
|
*/
|
2007-06-14 22:15:33 +04:00
|
|
|
|
2007-06-14 20:01:15 +04:00
|
|
|
return $this->_pending[$alias];
|
2007-05-20 22:40:53 +04:00
|
|
|
}
|
2007-05-22 00:06:23 +04:00
|
|
|
/**
|
|
|
|
* getRelation
|
|
|
|
*
|
|
|
|
* @param string $alias relation alias
|
|
|
|
*/
|
|
|
|
public function getRelation($alias, $recursive = true)
|
2007-05-21 00:35:21 +04:00
|
|
|
{
|
2007-05-22 00:06:23 +04:00
|
|
|
if (isset($this->_relations[$alias])) {
|
|
|
|
return $this->_relations[$alias];
|
2007-05-21 00:35:21 +04:00
|
|
|
}
|
|
|
|
|
2007-05-22 00:06:23 +04:00
|
|
|
if (isset($this->_pending[$alias])) {
|
|
|
|
$def = $this->_pending[$alias];
|
2007-05-21 01:18:52 +04:00
|
|
|
|
2007-05-22 19:57:17 +04:00
|
|
|
// check if reference class name exists
|
|
|
|
// if it does we are dealing with association relation
|
2007-05-21 01:18:52 +04:00
|
|
|
if (isset($def['refClass'])) {
|
|
|
|
$def = $this->completeAssocDefinition($def);
|
2007-05-22 00:06:23 +04:00
|
|
|
$localClasses = array_merge($this->_table->getOption('parents'), array($this->_table->getComponentName()));
|
|
|
|
|
|
|
|
if ( ! isset($this->_pending[$def['refClass']]) &&
|
2007-06-14 22:15:33 +04:00
|
|
|
! isset($this->_relations[$def['refClass']])) {
|
2007-06-14 20:01:15 +04:00
|
|
|
|
2007-07-13 20:22:49 +04:00
|
|
|
$parser = $def['refTable']->getRelationParser();
|
|
|
|
if ( ! $parser->hasRelation($this->_table->getComponentName())) {
|
|
|
|
$parser->bind($this->_table->getComponentName(),
|
|
|
|
array('type' => Doctrine_Relation::ONE,
|
|
|
|
'local' => $def['local'],
|
|
|
|
'foreign' => $this->_table->getIdentifier(),
|
|
|
|
'localKey' => true,
|
|
|
|
));
|
|
|
|
}
|
2007-06-14 20:01:15 +04:00
|
|
|
|
2007-07-13 20:22:49 +04:00
|
|
|
if ( ! $this->hasRelation($def['refClass'])) {
|
|
|
|
$this->bind($def['refClass'], array('type' => Doctrine_Relation::MANY,
|
|
|
|
'foreign' => $def['local'],
|
|
|
|
'local' => $this->_table->getIdentifier()));
|
|
|
|
}
|
2007-05-22 00:06:23 +04:00
|
|
|
}
|
|
|
|
if (in_array($def['class'], $localClasses)) {
|
2007-06-11 23:27:16 +04:00
|
|
|
$rel = new Doctrine_Relation_Nest($def);
|
2007-05-22 00:06:23 +04:00
|
|
|
} else {
|
2007-05-22 01:52:14 +04:00
|
|
|
$rel = new Doctrine_Relation_Association($def);
|
2007-05-22 00:06:23 +04:00
|
|
|
}
|
2007-05-21 01:18:52 +04:00
|
|
|
} else {
|
2007-05-22 19:57:17 +04:00
|
|
|
// simple foreign key relation
|
2007-05-21 01:18:52 +04:00
|
|
|
$def = $this->completeDefinition($def);
|
2007-05-22 19:57:17 +04:00
|
|
|
|
|
|
|
if (isset($def['localKey'])) {
|
2007-07-08 23:19:53 +04:00
|
|
|
|
2007-05-22 19:57:17 +04:00
|
|
|
$rel = new Doctrine_Relation_LocalKey($def);
|
|
|
|
} else {
|
|
|
|
$rel = new Doctrine_Relation_ForeignKey($def);
|
|
|
|
}
|
2007-05-21 01:18:52 +04:00
|
|
|
}
|
2007-05-22 01:52:14 +04:00
|
|
|
if (isset($rel)) {
|
2007-05-22 19:57:17 +04:00
|
|
|
// unset pending relation
|
|
|
|
unset($this->_pending[$alias]);
|
|
|
|
|
|
|
|
$this->_relations[$alias] = $rel;
|
2007-05-22 01:52:14 +04:00
|
|
|
return $rel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($recursive) {
|
2007-06-15 01:22:28 +04:00
|
|
|
$this->getRelations();
|
2007-06-26 01:36:45 +04:00
|
|
|
|
2007-05-24 01:13:13 +04:00
|
|
|
return $this->getRelation($alias, false);
|
2007-05-22 01:52:14 +04:00
|
|
|
} else {
|
2007-05-24 01:13:13 +04:00
|
|
|
throw new Doctrine_Table_Exception('Unknown relation alias ' . $alias);
|
2007-05-21 01:18:52 +04:00
|
|
|
}
|
|
|
|
}
|
2007-05-22 01:52:14 +04:00
|
|
|
/**
|
|
|
|
* getRelations
|
|
|
|
* returns an array containing all relation objects
|
|
|
|
*
|
|
|
|
* @return array an array of Doctrine_Relation objects
|
|
|
|
*/
|
|
|
|
public function getRelations()
|
|
|
|
{
|
|
|
|
foreach ($this->_pending as $k => $v) {
|
|
|
|
$this->getRelation($k);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->_relations;
|
|
|
|
}
|
2007-08-03 15:52:24 +04:00
|
|
|
/**
|
|
|
|
* getImpl
|
|
|
|
* returns the table class of the concrete implementation for given template
|
|
|
|
* if the given template is not a template then this method just returns the
|
|
|
|
* table class for the given record
|
|
|
|
*
|
|
|
|
* @param string $template
|
|
|
|
*/
|
|
|
|
public function getImpl($template)
|
|
|
|
{
|
2007-09-03 18:57:18 +04:00
|
|
|
$conn = $this->_table->getConnection();
|
2007-08-03 15:52:24 +04:00
|
|
|
|
|
|
|
if (in_array('Doctrine_Template', class_parents($template))) {
|
|
|
|
$impl = $this->_table->getImpl($template);
|
|
|
|
|
|
|
|
if ($impl === null) {
|
|
|
|
throw new Doctrine_Relation_Parser_Exception("Couldn't find concrete implementation for template " . $template);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$impl = $template;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $conn->getTable($impl);
|
|
|
|
}
|
2007-05-21 01:35:49 +04:00
|
|
|
/**
|
|
|
|
* Completes the given association definition
|
|
|
|
*
|
|
|
|
* @param array $def definition array to be completed
|
|
|
|
* @return array completed definition array
|
|
|
|
*/
|
2007-05-21 01:18:52 +04:00
|
|
|
public function completeAssocDefinition($def)
|
|
|
|
{
|
2007-09-03 18:57:18 +04:00
|
|
|
$conn = $this->_table->getConnection();
|
2007-08-03 15:52:24 +04:00
|
|
|
$def['table'] = $this->getImpl($def['class']);
|
|
|
|
$def['class'] = $def['table']->getComponentName();
|
|
|
|
$def['refTable'] = $this->getImpl($def['refClass']);
|
2007-05-21 00:35:21 +04:00
|
|
|
|
2007-06-10 01:54:08 +04:00
|
|
|
$id = $def['refTable']->getIdentifier();
|
|
|
|
|
|
|
|
if (count($id) > 1) {
|
|
|
|
if ( ! isset($def['foreign'])) {
|
|
|
|
// foreign key not set
|
|
|
|
// try to guess the foreign key
|
|
|
|
|
|
|
|
$def['foreign'] = ($def['local'] === $id[0]) ? $id[1] : $id[0];
|
|
|
|
}
|
|
|
|
if ( ! isset($def['local'])) {
|
|
|
|
// foreign key not set
|
|
|
|
// try to guess the foreign key
|
2007-05-21 00:35:21 +04:00
|
|
|
|
2007-06-10 01:54:08 +04:00
|
|
|
$def['local'] = ($def['foreign'] === $id[0]) ? $id[1] : $id[0];
|
|
|
|
}
|
|
|
|
} else {
|
2007-05-21 01:18:52 +04:00
|
|
|
|
2007-06-10 01:54:08 +04:00
|
|
|
if ( ! isset($def['foreign'])) {
|
|
|
|
// foreign key not set
|
|
|
|
// try to guess the foreign key
|
|
|
|
|
|
|
|
$columns = $this->getIdentifiers($def['table']);
|
|
|
|
|
|
|
|
$def['foreign'] = $columns;
|
|
|
|
}
|
|
|
|
if ( ! isset($def['local'])) {
|
|
|
|
// local key not set
|
|
|
|
// try to guess the local key
|
|
|
|
$columns = $this->getIdentifiers($this->_table);
|
|
|
|
|
|
|
|
$def['local'] = $columns;
|
|
|
|
}
|
2007-05-21 00:35:21 +04:00
|
|
|
}
|
2007-05-21 01:18:52 +04:00
|
|
|
return $def;
|
|
|
|
}
|
2007-05-21 01:35:49 +04:00
|
|
|
/**
|
|
|
|
* getIdentifiers
|
|
|
|
* gives a list of identifiers from given table
|
|
|
|
*
|
|
|
|
* the identifiers are in format:
|
|
|
|
* [componentName].[identifier]
|
|
|
|
*
|
|
|
|
* @param Doctrine_Table $table table object to retrieve identifiers from
|
|
|
|
*/
|
2007-05-21 01:18:52 +04:00
|
|
|
public function getIdentifiers(Doctrine_Table $table)
|
|
|
|
{
|
2007-09-03 18:57:18 +04:00
|
|
|
if (is_array($table->getIdentifier())) {
|
2007-05-21 01:18:52 +04:00
|
|
|
$columns = array();
|
|
|
|
foreach((array) $table->getIdentifier() as $identifier) {
|
|
|
|
$columns[] = strtolower($table->getComponentName())
|
|
|
|
. '_' . $table->getIdentifier();
|
|
|
|
}
|
2007-09-03 18:57:18 +04:00
|
|
|
} else {
|
2007-05-21 01:18:52 +04:00
|
|
|
$columns = strtolower($table->getComponentName())
|
|
|
|
. '_' . $table->getIdentifier();
|
2007-09-03 18:57:18 +04:00
|
|
|
}
|
2007-05-21 01:18:52 +04:00
|
|
|
|
|
|
|
return $columns;
|
2007-05-21 00:35:21 +04:00
|
|
|
}
|
2007-05-22 00:27:56 +04:00
|
|
|
/**
|
|
|
|
* guessColumns
|
|
|
|
*
|
|
|
|
* @param array $classes an array of class names
|
|
|
|
* @param Doctrine_Table $foreignTable foreign table object
|
|
|
|
* @return array an array of column names
|
|
|
|
*/
|
|
|
|
public function guessColumns(array $classes, Doctrine_Table $foreignTable)
|
2007-05-21 21:35:20 +04:00
|
|
|
{
|
|
|
|
$conn = $this->_table->getConnection();
|
|
|
|
|
|
|
|
foreach ($classes as $class) {
|
2007-06-07 23:20:24 +04:00
|
|
|
try {
|
|
|
|
$table = $conn->getTable($class);
|
|
|
|
} catch (Doctrine_Table_Exception $e) {
|
|
|
|
continue;
|
|
|
|
}
|
2007-05-21 21:35:20 +04:00
|
|
|
$columns = $this->getIdentifiers($table);
|
|
|
|
$found = true;
|
|
|
|
|
|
|
|
foreach ((array) $columns as $column) {
|
|
|
|
if ( ! $foreignTable->hasColumn($column)) {
|
|
|
|
$found = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($found) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! $found) {
|
|
|
|
throw new Doctrine_Relation_Exception("Couldn't find columns.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $columns;
|
|
|
|
}
|
2007-05-21 01:35:49 +04:00
|
|
|
/**
|
|
|
|
* Completes the given definition
|
|
|
|
*
|
|
|
|
* @param array $def definition array to be completed
|
|
|
|
* @return array completed definition array
|
|
|
|
*/
|
2007-05-21 00:35:21 +04:00
|
|
|
public function completeDefinition($def)
|
|
|
|
{
|
2007-09-03 18:57:18 +04:00
|
|
|
$conn = $this->_table->getConnection();
|
2007-08-03 15:52:24 +04:00
|
|
|
$def['table'] = $this->getImpl($def['class']);
|
|
|
|
$def['class'] = $def['table']->getComponentName();
|
|
|
|
|
2007-05-21 21:35:20 +04:00
|
|
|
$foreignClasses = array_merge($def['table']->getOption('parents'), array($def['class']));
|
|
|
|
$localClasses = array_merge($this->_table->getOption('parents'), array($this->_table->getComponentName()));
|
2007-05-21 00:35:21 +04:00
|
|
|
|
|
|
|
if (isset($def['local'])) {
|
|
|
|
if ( ! isset($def['foreign'])) {
|
|
|
|
// local key is set, but foreign key is not
|
|
|
|
// try to guess the foreign key
|
|
|
|
|
2007-05-21 21:35:20 +04:00
|
|
|
if ($def['local'] === $this->_table->getIdentifier()) {
|
|
|
|
$def['foreign'] = $this->guessColumns($localClasses, $def['table']);
|
2007-05-21 00:35:21 +04:00
|
|
|
} else {
|
|
|
|
// the foreign field is likely to be the
|
|
|
|
// identifier of the foreign class
|
|
|
|
$def['foreign'] = $def['table']->getIdentifier();
|
2007-05-22 19:57:17 +04:00
|
|
|
$def['localKey'] = true;
|
2007-05-21 00:35:21 +04:00
|
|
|
}
|
2007-06-26 01:36:45 +04:00
|
|
|
} else {
|
2007-07-08 23:19:53 +04:00
|
|
|
if ($def['local'] !== $this->_table->getIdentifier() &&
|
|
|
|
$def['type'] == Doctrine_Relation::ONE) {
|
2007-06-27 02:35:08 +04:00
|
|
|
$def['localKey'] = true;
|
2007-06-26 01:36:45 +04:00
|
|
|
}
|
2007-06-28 15:56:56 +04:00
|
|
|
}
|
2007-05-21 00:35:21 +04:00
|
|
|
} else {
|
|
|
|
if (isset($def['foreign'])) {
|
|
|
|
// local key not set, but foreign key is set
|
|
|
|
// try to guess the local key
|
2007-05-21 21:35:20 +04:00
|
|
|
if ($def['foreign'] === $def['table']->getIdentifier()) {
|
2007-05-22 19:57:17 +04:00
|
|
|
$def['localKey'] = true;
|
2007-06-07 23:20:24 +04:00
|
|
|
try {
|
|
|
|
$def['local'] = $this->guessColumns($foreignClasses, $this->_table);
|
|
|
|
} catch (Doctrine_Relation_Exception $e) {
|
|
|
|
$def['local'] = $this->_table->getIdentifier();
|
|
|
|
}
|
2007-05-21 00:35:21 +04:00
|
|
|
} else {
|
2007-05-21 21:35:20 +04:00
|
|
|
$def['local'] = $this->_table->getIdentifier();
|
2007-05-21 00:35:21 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// neither local or foreign key is being set
|
|
|
|
// try to guess both keys
|
|
|
|
|
2007-05-22 00:06:23 +04:00
|
|
|
$conn = $this->_table->getConnection();
|
2007-05-21 00:35:21 +04:00
|
|
|
|
2007-05-22 00:06:23 +04:00
|
|
|
// the following loops are needed for covering inheritance
|
|
|
|
foreach ($localClasses as $class) {
|
|
|
|
$table = $conn->getTable($class);
|
|
|
|
$column = strtolower($table->getComponentName())
|
|
|
|
. '_' . $table->getIdentifier();
|
2007-06-28 23:19:47 +04:00
|
|
|
|
2007-05-22 00:06:23 +04:00
|
|
|
foreach ($foreignClasses as $class2) {
|
|
|
|
$table2 = $conn->getTable($class2);
|
|
|
|
if ($table2->hasColumn($column)) {
|
|
|
|
$def['foreign'] = $column;
|
|
|
|
$def['local'] = $table->getIdentifier();
|
2007-05-22 21:42:47 +04:00
|
|
|
|
2007-05-22 00:06:23 +04:00
|
|
|
return $def;
|
|
|
|
}
|
2007-05-21 00:35:21 +04:00
|
|
|
}
|
|
|
|
}
|
2007-05-20 22:40:53 +04:00
|
|
|
|
2007-05-22 00:06:23 +04:00
|
|
|
foreach ($foreignClasses as $class) {
|
|
|
|
$table = $conn->getTable($class);
|
|
|
|
$column = strtolower($table->getComponentName())
|
|
|
|
. '_' . $table->getIdentifier();
|
|
|
|
|
|
|
|
foreach ($localClasses as $class2) {
|
|
|
|
$table2 = $conn->getTable($class2);
|
|
|
|
if ($table2->hasColumn($column)) {
|
2007-06-28 23:19:47 +04:00
|
|
|
$def['foreign'] = $table->getIdentifier();
|
|
|
|
$def['local'] = $column;
|
2007-05-22 21:42:47 +04:00
|
|
|
$def['localKey'] = true;
|
2007-05-22 00:06:23 +04:00
|
|
|
return $def;
|
2007-05-20 22:40:53 +04:00
|
|
|
}
|
|
|
|
}
|
2007-05-22 19:57:17 +04:00
|
|
|
}
|
2007-06-28 23:19:47 +04:00
|
|
|
|
|
|
|
// auto-add columns and auto-build relation
|
|
|
|
$columns = array();
|
|
|
|
foreach ((array) $this->_table->getIdentifier() as $id) {
|
|
|
|
$column = strtolower($table->getComponentName())
|
|
|
|
. '_' . $id;
|
|
|
|
|
|
|
|
$col = $this->_table->getDefinitionOf($id);
|
|
|
|
$type = $col['type'];
|
|
|
|
$length = $col['length'];
|
|
|
|
|
|
|
|
unset($col['type']);
|
|
|
|
unset($col['length']);
|
|
|
|
unset($col['autoincrement']);
|
|
|
|
unset($col['sequence']);
|
|
|
|
unset($col['primary']);
|
|
|
|
|
|
|
|
$def['table']->setColumn($column, $type, $length, $col);
|
|
|
|
|
|
|
|
$columns[] = $column;
|
|
|
|
}
|
|
|
|
if (count($columns) > 1) {
|
|
|
|
$def['foreign'] = $columns;
|
|
|
|
} else {
|
|
|
|
$def['foreign'] = $columns[0];
|
|
|
|
}
|
|
|
|
$def['local'] = $this->_table->getIdentifier();
|
2007-05-22 00:06:23 +04:00
|
|
|
}
|
2007-05-20 22:40:53 +04:00
|
|
|
}
|
2007-05-22 00:06:23 +04:00
|
|
|
return $def;
|
2007-05-20 22:40:53 +04:00
|
|
|
}
|
2007-10-04 01:43:22 +04:00
|
|
|
}
|