system/classes/Model.php
protected function _getQueryObjectForRelation($attribute)
    {
      // Setup
      $def = $this->model_attributes[$attribute];
      
      // If not a model relationship, just return an adaptor for this model
      if (!isset($def['model']) && !isset($def['models'])) {
        return $this->getDbAdaptor();
      }

      // Get DB adaptor to use for model relationships
      $relatedObj = isset($def['models']) ? $this->fetchRelatedObj($def['models']) : $this->fetchRelatedObj($def['model']);
      $query = $relatedObj->getDbAdaptor();

      // If the relationship is many-to-many and uses a relation table.
      // OR if the relationship is one-to-many and no 'owner' type column is set,
      // meaning there needs to be a relation table.
      // If 'via' or 'using' is not set, then it's assumed the relation utilizes a relation table.
      if (!isset($def['via']) && !isset($def['using'])) {
        
        // Grab relation table name
        $relTable = $this->getRelationTableName($relatedObj, $attribute, $this->model_attributes[$attribute]);

        // In situations where multiple DBs are being used and there's a relation table 
        // between data on different DBs, we can't be sure which DB holds the relation table. 
        // First try the DB the related object is on. If that doesn't contain the relation table,
        // then try the current object's DB.
        if (!$query->tableExists($relTable)) {
          $query = $this->getDbAdaptor();
        }
      }
      return $query;
    }
protected function _getQueryObjectForRelation($attribute)
    {
      // Setup
      $def = $this->model_attributes[$attribute];
      
      // If not a model relationship, just return an adaptor for this model
      if (!isset($def['model']) && !isset($def['models'])) {
        return $this->getDbAdaptor();
      }

      // Get DB adaptor to use for model relationships
      $relatedObj = isset($def['models']) ? $this->fetchRelatedObj($def['models']) : $this->fetchRelatedObj($def['model']);
      $query = $relatedObj->getDbAdaptor();

      // If the relationship is many-to-many and uses a relation table.
      // OR if the relationship is one-to-many and no 'owner' type column is set,
      // meaning there needs to be a relation table.
      // If 'via' or 'using' is not set, then it's assumed the relation utilizes a relation table.
      if (!isset($def['via']) && !isset($def['using'])) {
        
        // Grab relation table name
        $relTable = $this->getRelationTableName($relatedObj, $attribute, $this->model_attributes[$attribute]);

        // In situations where multiple DBs are being used and there's a relation table 
        // between data on different DBs, we can't be sure which DB holds the relation table. 
        // First try the DB the related object is on. If that doesn't contain the relation table,
        // then try the current object's DB.
        if (!$query->tableExists($relTable)) {
          $query = $this->getDbAdaptor();
        }
      }
      return $query;
    }
Returns a query object for a specific related model.

@return QueryBuilder