/**
  * Constructor
  *
  * @param {DB} $db Handle to database
  * @param {string} $id ID of parent query - $id should refer to an atomic query
  * @param {string} $terms Query terms corresponding to the child queries of the tree
  */
 public function __construct(&$db, $id, $terms = array())
 {
     parent::__construct($db);
     // Create the root query
     $this->base = $this->db->query($id);
     // Ensure root query fields are copied to this query
     $this->update();
     // Check for child aliases
     $aliases = array();
     foreach ($this->output->fields as $field => $spec) {
         if (property_exists($spec, 'child') && $spec->child != $field) {
             $aliases[$field] = $spec->child;
         }
     }
     $terms = Term::convertAliases($terms, $aliases);
     // Add root or id as filter to each child
     $linkID = $this->prefix();
     if (!$linkID) {
         throw new \Exception("Query '{$id}' has no prefix");
     }
     for ($i = 0; $i < count($terms); $i++) {
         $terms[$i] = "(" . $terms[$i] . "):" . $linkID;
     }
     // Create a child query for each term
     $this->linkList($terms, false);
 }