$this->_pending[$alias] = $options;
}
- public function getRelation($alias, $recursive = true)
+ private function createRelation($alias)
{
- if (isset($this->_relations[$alias])) {
- return $this->_relations[$alias];
- }
+ if (isset($this->_relations[$alias]))
+ return;
- if (isset($this->_pending[$alias])) {
- $def = $this->_pending[$alias];
- $identifierColumnNames = $this->_table->getIdentifierColumnNames();
- $idColumnName = array_pop($identifierColumnNames);
-
- // check if reference class name exists
- // if it does we are dealing with association relation
- if (isset($def['refClass'])) {
- $def = $this->completeAssocDefinition($def);
- $localClasses = array_merge($this->_table->getOption('parents'), array($this->_table->getComponentName()));
-
- if ( ! isset($this->_pending[$def['refClass']]) &&
- ! isset($this->_relations[$def['refClass']])) {
-
- $parser = $def['refTable']->getRelationParser();
- if ( ! $parser->hasRelation($this->_table->getComponentName())) {
- $parser->bind($this->_table->getComponentName(), null, IPF_ORM_Relation::ONE, array(
- 'local' => $def['local'],
- 'foreign' => $idColumnName,
- 'localKey' => true,
- ));
- }
+ if (!isset($this->_pending[$alias]))
+ throw new IPF_ORM_Exception('Unknown relation alias "' . $alias . '".');
- if (!$this->hasRelation($def['refClass'])) {
- $this->bind($def['refClass'], null, IPF_ORM_Relation::MANY, array(
- 'foreign' => $def['local'],
- 'local' => $idColumnName,
- ));
- }
+ $def = $this->_pending[$alias];
+ $identifierColumnNames = $this->_table->getIdentifierColumnNames();
+ $idColumnName = array_pop($identifierColumnNames);
+
+ // check if reference class name exists
+ // if it does we are dealing with association relation
+ if (isset($def['refClass'])) {
+ $def = $this->completeAssocDefinition($def);
+ $localClasses = array_merge($this->_table->getOption('parents'), array($this->_table->getComponentName()));
+
+ if ( ! isset($this->_pending[$def['refClass']]) &&
+ ! isset($this->_relations[$def['refClass']])) {
+
+ $parser = $def['refTable']->getRelationParser();
+ if ( ! $parser->hasRelation($this->_table->getComponentName())) {
+ $parser->bind($this->_table->getComponentName(), null, IPF_ORM_Relation::ONE, array(
+ 'local' => $def['local'],
+ 'foreign' => $idColumnName,
+ 'localKey' => true,
+ ));
}
- if (in_array($def['class'], $localClasses)) {
- $rel = new IPF_ORM_Relation_Nest($def);
- } else {
- $rel = new IPF_ORM_Relation_Association($def);
+
+ if (!$this->hasRelation($def['refClass'])) {
+ $this->bind($def['refClass'], null, IPF_ORM_Relation::MANY, array(
+ 'foreign' => $def['local'],
+ 'local' => $idColumnName,
+ ));
}
+ }
+ if (in_array($def['class'], $localClasses)) {
+ $rel = new IPF_ORM_Relation_Nest($def);
} else {
- // simple foreign key relation
- $def = $this->completeDefinition($def);
+ $rel = new IPF_ORM_Relation_Association($def);
+ }
+ } else {
+ // simple foreign key relation
+ $def = $this->completeDefinition($def);
- if (isset($def['localKey'])) {
- $rel = new IPF_ORM_Relation_LocalKey($def);
+ if (isset($def['localKey'])) {
+ $rel = new IPF_ORM_Relation_LocalKey($def);
- // Automatically index foreign keys which are not primary
- $foreign = (array) $def['foreign'];
- foreach ($foreign as $fk) {
- if ( ! $rel->getTable()->isIdentifier($fk)) {
- $rel->getTable()->addIndex($fk, array('fields' => array($fk)));
- }
+ // Automatically index foreign keys which are not primary
+ $foreign = (array) $def['foreign'];
+ foreach ($foreign as $fk) {
+ if ( ! $rel->getTable()->isIdentifier($fk)) {
+ $rel->getTable()->addIndex($fk, array('fields' => array($fk)));
}
- } else {
- $rel = new IPF_ORM_Relation_ForeignKey($def);
}
- }
- if (isset($rel)) {
- // unset pending relation
- unset($this->_pending[$alias]);
-
- $this->_relations[$alias] = $rel;
- return $rel;
+ } else {
+ $rel = new IPF_ORM_Relation_ForeignKey($def);
}
}
- if ($recursive) {
- $this->getRelations();
- return $this->getRelation($alias, false);
- } else {
- throw new IPF_ORM_Exception('Unknown relation alias "' . $alias . '".');
+
+ if (isset($rel)) {
+ // unset pending relation
+ unset($this->_pending[$alias]);
+
+ $this->_relations[$alias] = $rel;
}
}
- public function getRelations()
+ public function getRelation($alias)
{
- foreach ($this->_pending as $k => $v) {
- $this->getRelation($k);
- }
-
- return $this->_relations;
+ $this->getRelations();
+ return $this->_relations[$alias];
}
- public function getImpl($template)
+ public function getRelations()
{
- $conn = $this->_table->getConnection();
-
- if (in_array('IPF_ORM_Template', class_parents($template))) {
- $impl = $this->_table->getImpl($template);
-
- if ($impl === null) {
- throw new IPF_ORM_Exception("Couldn't find concrete implementation for template " . $template);
- }
- } else {
- $impl = $template;
+ foreach ($this->_pending as $k => $v) {
+ $this->createRelation($k);
}
- return $conn->getTable($impl);
+ return $this->_relations;
}
public function completeAssocDefinition($def)
{
$conn = $this->_table->getConnection();
- $def['table'] = $this->getImpl($def['class']);
- $def['localTable'] = $this->_table;
- $def['class'] = $def['table']->getComponentName();
- $def['refTable'] = $this->getImpl($def['refClass']);
+
+ $def['table'] = IPF_ORM::getTable($def['class']);
+ $def['localTable'] = $this->_table;
+ $def['class'] = $def['table']->getComponentName();
+ $def['refTable'] = IPF_ORM::getTable($def['refClass']);
$id = $def['refTable']->getIdentifierColumnNames();
public function completeDefinition($def)
{
$conn = $this->_table->getConnection();
- $def['table'] = $this->getImpl($def['class']);
- $def['localTable'] = $this->_table;
- $def['class'] = $def['table']->getComponentName();
+ $def['table'] = IPF_ORM::getTable($def['class']);
+ $def['localTable'] = $this->_table;
+ $def['class'] = $def['table']->getComponentName();
$foreignClasses = array_merge($def['table']->getOption('parents'), array($def['class']));
$localClasses = array_merge($this->_table->getOption('parents'), array($this->_table->getComponentName()));