public function __get($key) { if (isset(SalamaData::$c[$key])) { // $model->RelatedModel (disallowed for now) throw new Exception("Unknown relation"); } elseif ($this->isRelation($key)) { // $model->relation if (!($model = $this->hasRelationData($this->getQuery()->model, $key))) { throw new Exception("No relation found for {$model} AND {$this->getQuery()->model}->{$key}"); } $this->evaluateLazyCall($this->getQuery()); $pk = $this->getPk(); $salama = $this->getModelInstance($model); SalamaQuery::add($salama, $model, $key); $map = SalamaRelation::getMap($this->getQuery()->model, $key, $salama->getQuery()->model); $salama = $salama->where(q::$map['localkey']($this->{$map}['foreignkey'])); } else { // $model->field $query = $this->getQuery(); if (!isset(SalamaData::$c[$query->model][$key])) { throw new Exception("Invalid model field/relation {$query->model}->{$key}"); } $this->evaluateLazyCall($query); return $query->getItem($key); } return $salama; }
function setupJoin($args, $query) { if (count($args) == 1) { $from = $query->model; } else { $from = $args[1]; } $from_alias = $this->builder->getAlias($from, $query); $type = 'LEFT'; foreach ($args as $key => $model) { $model_alias = $this->builder->getAlias($model, $query); $map = SalamaRelation::getMap($model, $from); $model_fk = $map['foreignkey']; $from_pk = SalamaBuild::getPk($from); $result[] = "{$type} JOIN {$model} {$model_alias} ON {$from_alias}.{$from_pk}={$model_alias}.{$model_fk}"; $query->_involved_tables[] = array('table' => $model, 'alias' => $model_alias); } return $result; }