/** * getForeignClass * * tries to return the class that should be loaded as a foreign object * * @param string $field * @throws Exception if field is not a foreign key into the table * @return string a valid class name */ public function getForeignClass($field) { if ($this->isForeignKey($field)) { if (isset($this->_foreign_keys[$field]['alias'])) { if (isset($this->_foreign_keys[$field]['class_from_field'])) { return $this->getForeignClassFromField($this->_foreign_keys[$field]['class_from_field']); } else { if (MormConf::isInConf($this->_foreign_keys[$field]['alias'])) { $table_name = $this->_foreign_keys[$field]['alias']; } else { $table_name = $this->_foreign_keys[$field]['table']; } } } else { $table_name = $this->_foreign_keys[$field]['table']; } return MormConf::generateMormClass($table_name); } else { throw new Exception($field . ' is not a foreign key in table ' . $this->_table); } }
/** * set_join * * TODO take $type in account to define if join is RIGHT, LEFT, INNER, OUTER etc. * * @param mixed $table * @param mixed $on * @param string $type * @return void */ public function set_join($table_or_alias, $on = null, $type = 'LEFT') { $table = $this->getJoinableTable($table_or_alias); if (!$this->is_used_table($table)) { $table = MormConf::isInConf($table_or_alias) ? $this->add_table($table_or_alias) : $this->add_table($table); $this->join_tables[] = $table; } if (!is_null($on)) { $tables = array_keys($on); $this->joins[] = array(array($tables[0] => $tables[1]), $on); } else { $key = $this->base_models[$this->base_table]->getForeignKeyFrom($table_or_alias); try { $ft_key = $this->base_models[$this->base_table]->getForeignTableKey($key); } catch (Exception $e) { if ($this->base_models[$this->base_table]->isForeignUsingTable($table)) { $ft_key = $this->base_models[$this->base_table]->getForeignMormonsUsingKey($table); } else { $ft_key = $this->base_models[$this->base_table]->getForeignMormonsKey($table); } } $this->joins[] = array(array($this->base_table => $table), array($this->base_table => $key, $table => $ft_key)); } //@todo put executed tu false only when join has changed $this->_executed = false; // switch($type) // { // case 'LEFT': // break; // case 'RIGHT': // break; // default: // throw new Exception("The join type ".$type." does not exist or is not yet supported by Mormons"); // break; // } }