/** * Connects a query to this query * * @param {string} $term * * @return {Query} */ protected function link($term) { $child = parent::link($term); // If parent is compiled we are ready (child specs are set by compiler) if (get_class($this->base) == "TinyQueries\\QuerySQL") { return $child; } // Find the matching key between parent & child $queries = array($this->base, $child); $matchKey = $this->match($queries); if (!$matchKey) { throw new \Exception("Tree::link - cannot link query; there is no unique matching key for '" . $this->base->name() . "' and '" . $child->name() . "'"); } $parentKey = $this->keys->{$matchKey}; $childKey = $child->keys->{$matchKey}; $parentKeyAlias = "__parentKey-" . count($this->children); $childKeyAlias = $matchKey; // Add parentKey to select $this->base->addSelect($parentKey, $parentKeyAlias); // Create child definition which is compatible with the one used for compiled queries $childDef = new \StdClass(); $childDef->type = 'child'; $childDef->child = $child->name(); $childDef->parentKey = $parentKeyAlias; $childDef->childKey = $childKeyAlias; $childDef->params = new \StdClass(); $childDef->params->{$matchKey} = $parentKeyAlias; $this->output->fields->{$child->name()} = $childDef; // Modify child such that it can be linked to the parent $child->bind($matchKey, $childKey); return $child; }