foreign key support
This commit is contained in:
parent
5006400cf9
commit
82b9d0739c
@ -47,12 +47,14 @@ class Doctrine_Formatter extends Doctrine_Connection_Module
|
|||||||
public function escapePattern($text)
|
public function escapePattern($text)
|
||||||
{
|
{
|
||||||
if ($this->string_quoting['escape_pattern']) {
|
if ($this->string_quoting['escape_pattern']) {
|
||||||
$text = str_replace($this->string_quoting['escape_pattern'],
|
$tmp = $this->conn->string_quoting;
|
||||||
$this->string_quoting['escape_pattern'] .
|
|
||||||
$this->string_quoting['escape_pattern'], $text);
|
$text = str_replace($tmp['escape_pattern'],
|
||||||
|
$tmp['escape_pattern'] .
|
||||||
|
$tmp['escape_pattern'], $text);
|
||||||
|
|
||||||
foreach ($this->wildcards as $wildcard) {
|
foreach ($this->wildcards as $wildcard) {
|
||||||
$text = str_replace($wildcard, $this->string_quoting['escape_pattern'] . $wildcard, $text);
|
$text = str_replace($wildcard, $tmp['escape_pattern'] . $wildcard, $text);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $text;
|
return $text;
|
||||||
|
@ -351,28 +351,146 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable
|
|||||||
$primary[] = $name;
|
$primary[] = $name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
$options['foreignKeys'] = array();
|
||||||
|
|
||||||
if ($parseForeignKeys) {
|
if ($parseForeignKeys) {
|
||||||
if ($this->getAttribute(Doctrine::ATTR_EXPORT) & Doctrine::EXPORT_CONSTRAINTS) {
|
if ($this->getAttribute(Doctrine::ATTR_EXPORT) & Doctrine::EXPORT_CONSTRAINTS) {
|
||||||
|
|
||||||
|
$constraints = array();
|
||||||
|
|
||||||
|
$emptyIntegrity = array('onUpdate' => null,
|
||||||
|
'onDelete' => null);
|
||||||
|
|
||||||
foreach ($this->getRelations() as $name => $relation) {
|
foreach ($this->getRelations() as $name => $relation) {
|
||||||
$fk = $relation->toArray();
|
$fk = $relation->toArray();
|
||||||
$fk['foreignTable'] = $relation->getTable()->getTableName();
|
$fk['foreignTable'] = $relation->getTable()->getTableName();
|
||||||
|
|
||||||
if ($relation->getTable() === $this && in_array($relation->getLocal(), $primary)) {
|
if ($relation->getTable() === $this && in_array($relation->getLocal(), $primary)) {
|
||||||
continue;
|
if ($relation->hasConstraint()) {
|
||||||
|
throw new Doctrine_Table_Exception("Badly constructed integrity constraints.");
|
||||||
|
}
|
||||||
|
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($relation->hasConstraint()) {
|
$integrity = array('onUpdate' => $fk['onUpdate'],
|
||||||
|
'onDelete' => $fk['onDelete']);
|
||||||
$options['foreignKeys'][] = $fk;
|
|
||||||
|
if ($relation instanceof Doctrine_Relation_ForeignKey) {
|
||||||
|
if ($relation->getLocal() !== $relation->getTable()->getIdentifier() &&
|
||||||
|
$relation->getLocal() !== $this->getIdentifier() ||
|
||||||
|
$relation->hasConstraint()) {
|
||||||
|
|
||||||
|
$def = array('local' => $relation->getLocal(),
|
||||||
|
'table' => $relation->getTable()->getTableName(),
|
||||||
|
'foreign' => $this->getIdentifier(),
|
||||||
|
'foreignTable' => $this->getTableName());
|
||||||
|
|
||||||
|
if (($key = array_search($def, $options['foreignKeys'])) === false) {
|
||||||
|
$options['foreignKeys'][] = $def;
|
||||||
|
|
||||||
|
$constraints[] = $integrity;
|
||||||
|
} else {
|
||||||
|
if ($integrity !== $emptyIntegrity) {
|
||||||
|
$constraints[$key] = $integrity;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
} elseif ($relation instanceof Doctrine_Relation_LocalKey) {
|
} elseif ($relation instanceof Doctrine_Relation_LocalKey) {
|
||||||
$options['foreignKeys'][] = $fk;
|
|
||||||
|
if ($relation->getLocal() !== $this->getIdentifier() &&
|
||||||
|
$relation->getForeign() !== $relation->getTable()->getIdentifier()) {
|
||||||
|
|
||||||
|
$def = array('local' => $relation->getLocal(),
|
||||||
|
'table' => $relation->getTable()->getTableName(),
|
||||||
|
'foreign' => $this->getIdentifier(),
|
||||||
|
'foreignTable' => $this->getTableName());
|
||||||
|
|
||||||
|
if (($key = array_search($def, $options['foreignKeys'])) === false) {
|
||||||
|
$options['foreignKeys'][] = $def;
|
||||||
|
|
||||||
|
$constraints[] = $integrity;
|
||||||
|
} else {
|
||||||
|
if ($integrity !== $emptyIntegrity) {
|
||||||
|
$constraints[$key] = $integrity;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} elseif ($relation instanceof Doctrine_Relation_Nest) {
|
||||||
|
/**
|
||||||
|
$def = array('local' => $relation->getLocal(),
|
||||||
|
'table' => $relation->getAssociationTable()->getTableName(),
|
||||||
|
'foreign' => $this->getIdentifier(),
|
||||||
|
'foreignTable' => $this->getTableName());
|
||||||
|
|
||||||
|
|
||||||
|
if (($key = array_search($def, $options['foreignKeys'])) === false) {
|
||||||
|
$options['foreignKeys'][] = $def;
|
||||||
|
|
||||||
|
$constraints[] = $integrity;
|
||||||
|
} else {
|
||||||
|
if ($integrity !== $emptyIntegrity) {
|
||||||
|
$constraints[$key] = $integrity;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$def = array('local' => $relation->getForeign(),
|
||||||
|
'table' => $relation->getAssociationTable()->getTableName(),
|
||||||
|
'foreign' => $this->getIdentifier(),
|
||||||
|
'foreignTable' => $relation->getTable()->getTableName());
|
||||||
|
|
||||||
|
if (($key = array_search($def, $options['foreignKeys'])) === false) {
|
||||||
|
$options['foreignKeys'][] = $def;
|
||||||
|
|
||||||
|
if ( ! isset($integrity['onDelete'])) {
|
||||||
|
$integrity['onDelete'] = 'CASCADE';
|
||||||
|
}
|
||||||
|
|
||||||
|
$constraints[] = $integrity;
|
||||||
|
} else {
|
||||||
|
if ($integrity !== $emptyIntegrity) {
|
||||||
|
if ( ! isset($integrity['onDelete'])) {
|
||||||
|
$integrity['onDelete'] = 'CASCADE';
|
||||||
|
}
|
||||||
|
|
||||||
|
$constraints[$key] = $integrity;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
} elseif ($relation instanceof Doctrine_Relation_Association) {
|
||||||
|
/**
|
||||||
|
$def = array('local' => $relation->getLocal(),
|
||||||
|
'table' => $relation->getAssociationTable()->getTableName(),
|
||||||
|
'foreign' => $this->getIdentifier(),
|
||||||
|
'foreignTable' => $this->getTableName());
|
||||||
|
if (($key = array_search($def, $options['foreignKeys'])) === false) {
|
||||||
|
$options['foreignKeys'][] = $def;
|
||||||
|
|
||||||
|
if ( ! isset($integrity['onDelete'])) {
|
||||||
|
$integrity['onDelete'] = 'CASCADE';
|
||||||
|
}
|
||||||
|
|
||||||
|
$constraints[] = $integrity;
|
||||||
|
} else {
|
||||||
|
if ($integrity !== $emptyIntegrity) {
|
||||||
|
if ( ! isset($integrity['onDelete'])) {
|
||||||
|
$integrity['onDelete'] = 'CASCADE';
|
||||||
|
}
|
||||||
|
$constraints[$key] = $integrity;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
foreach ($constraints as $k => $def) {
|
||||||
|
$options['foreignKeys'][$k] = array_merge($options['foreignKeys'][$k], $def);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$options['primary'] = $primary;
|
$options['primary'] = $primary;
|
||||||
|
|
||||||
return array('tableName' => $this->getOption('tableName'),
|
return array('tableName' => $this->getOption('tableName'),
|
||||||
'columns' => $columns,
|
'columns' => $columns,
|
||||||
@ -1057,51 +1175,6 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable
|
|||||||
|
|
||||||
return array_search($value, $values);
|
return array_search($value, $values);
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
* invokeSet
|
|
||||||
*
|
|
||||||
* @param mixed $value
|
|
||||||
*/
|
|
||||||
public function invokeSet(Doctrine_Record $record, $name, $value)
|
|
||||||
{
|
|
||||||
if ( ! ($this->getAttribute(Doctrine::ATTR_ACCESSORS) & Doctrine::ACCESSOR_SET)) {
|
|
||||||
return $value;
|
|
||||||
}
|
|
||||||
$prefix = $this->getAttribute(Doctrine::ATTR_ACCESSOR_PREFIX_SET);
|
|
||||||
if (!$prefix)
|
|
||||||
$prefix = 'set';
|
|
||||||
|
|
||||||
$method = $prefix . $name;
|
|
||||||
|
|
||||||
if (method_exists($record, $method)) {
|
|
||||||
return $record->$method($value);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $value;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* invokeGet
|
|
||||||
*
|
|
||||||
* @param mixed $value
|
|
||||||
*/
|
|
||||||
public function invokeGet(Doctrine_Record $record, $name, $value)
|
|
||||||
{
|
|
||||||
if ( ! ($this->getAttribute(Doctrine::ATTR_ACCESSORS) & Doctrine::ACCESSOR_GET)) {
|
|
||||||
return $value;
|
|
||||||
}
|
|
||||||
$prefix = $this->getAttribute(Doctrine::ATTR_ACCESSOR_PREFIX_GET);
|
|
||||||
if (!$prefix)
|
|
||||||
$prefix = 'get';
|
|
||||||
|
|
||||||
$method = $prefix . $name;
|
|
||||||
|
|
||||||
if (method_exists($record, $method)) {
|
|
||||||
return $record->$method($value);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* getDefinitionOf
|
* getDefinitionOf
|
||||||
*
|
*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user