system/classes/Model.php
protected function _getModel($attributeName, $relatedObjName = false, $query = false, $loadMap = false, $record = false)
    {
      $def = $this->model_attributes[$attributeName];
      $result = null;

      if ($relatedObjName) {

        // If a LoadMap is present, and explicit fetching of the data isn't enabled, and some data was passed in,
        // then use the data given.
        if ($loadMap instanceof \Cora\Adm\LoadMap && !$loadMap->fetchData() && $record !== false) {
          
          // Create a blank object of desired type and populate it with the results of the data passed in
          $relatedObj = $this->fetchRelatedObj($def['model']);        
          $result = $relatedObj->_populate($record, $query, $loadMap);
        }

        // If fetching via a defined column on a table.
        else if (isset($def['via'])) {
          $result = $this->_getModelFromTableColumn($attributeName, $def['model'], $def['via'], $query, $loadMap);
        }
        
        // If custom defined relationship for this single model
        else if (isset($def['using'])) {
          $result = $this->getModelFromCustomRelationship($attributeName, $def['model'], $query, $loadMap);
        }

        // In the rare case that we need to fetch a single related object, and the developer choose
        // to use a relation table to represent the relationship.
        // It's abstract in the sense that there's nothing on the current model's table 
        // leading to it. We need to grab it using our method to grab data from a relation table.
        else if (isset($def['usesRefTable'])) {
          $result = $this->_getModelFromRelationTable($attributeName, $def['model'], $query, $loadMap);
        }

        // In the more common case of fetching a single object, where the related object's
        // ID is stored in a column on the parent object.
        // Under this scenario, the value stored in $this->$name is the ID of the related
        // object that was already fetched. So we can use that ID to populate a blank
        // object and then rely on it's dynamic loading to fetch any additional needed info.
        else {
          // Create a blank object of desired type
          $relatedObj = $this->fetchRelatedObj($def['model']);        
          
          // If a custom query was passed in, execute it
          // Then populate a model with the data result
          if ($query && $query->isCustom()) {
            $data = $query->fetch();
            $result = $relatedObj->_populate($data);
          }

          else {
            // If the Identifier is not already loaded from table, then get the ID so we can use it to 
            // fetch the model.
            if (!isset($this->model_data[$attributeName])) {
              $this->model_data[$attributeName] = $this->_fetchData($attributeName);
            }  

            // Fetch related object in whole (The model_data we have on it should be an ID reference)
            if (!is_object($this->model_data[$attributeName])) {
              $relObjRepo = $relatedObj->getRepository(true);
              $result = $relObjRepo->find($this->model_data[$attributeName]);
            } 
            
            // Unless we already have an object (maybe it was added to the model from the main app)
            // Then just use what we have
            else {
              $result = $this->model_data[$attributeName];
            }

            // Incase there's loadMap info that needs to be passed in, call populate
            if ($result) {
              $result->_populate([], false, $loadMap);
            }
          }
        }
      }
      return $result;
    }
protected function _getModel($attributeName, $relatedObjName = false, $query = false, $loadMap = false, $record = false)
    {
      $def = $this->model_attributes[$attributeName];
      $result = null;

      if ($relatedObjName) {

        // If a LoadMap is present, and explicit fetching of the data isn't enabled, and some data was passed in,
        // then use the data given.
        if ($loadMap instanceof \Cora\Adm\LoadMap && !$loadMap->fetchData() && $record !== false) {
          
          // Create a blank object of desired type and populate it with the results of the data passed in
          $relatedObj = $this->fetchRelatedObj($def['model']);        
          $result = $relatedObj->_populate($record, $query, $loadMap);
        }

        // If fetching via a defined column on a table.
        else if (isset($def['via'])) {
          $result = $this->_getModelFromTableColumn($attributeName, $def['model'], $def['via'], $query, $loadMap);
        }
        
        // If custom defined relationship for this single model
        else if (isset($def['using'])) {
          $result = $this->getModelFromCustomRelationship($attributeName, $def['model'], $query, $loadMap);
        }

        // In the rare case that we need to fetch a single related object, and the developer choose
        // to use a relation table to represent the relationship.
        // It's abstract in the sense that there's nothing on the current model's table 
        // leading to it. We need to grab it using our method to grab data from a relation table.
        else if (isset($def['usesRefTable'])) {
          $result = $this->_getModelFromRelationTable($attributeName, $def['model'], $query, $loadMap);
        }

        // In the more common case of fetching a single object, where the related object's
        // ID is stored in a column on the parent object.
        // Under this scenario, the value stored in $this->$name is the ID of the related
        // object that was already fetched. So we can use that ID to populate a blank
        // object and then rely on it's dynamic loading to fetch any additional needed info.
        else {
          // Create a blank object of desired type
          $relatedObj = $this->fetchRelatedObj($def['model']);        
          
          // If a custom query was passed in, execute it
          // Then populate a model with the data result
          if ($query && $query->isCustom()) {
            $data = $query->fetch();
            $result = $relatedObj->_populate($data);
          }

          else {
            // If the Identifier is not already loaded from table, then get the ID so we can use it to 
            // fetch the model.
            if (!isset($this->model_data[$attributeName])) {
              $this->model_data[$attributeName] = $this->_fetchData($attributeName);
            }  

            // Fetch related object in whole (The model_data we have on it should be an ID reference)
            if (!is_object($this->model_data[$attributeName])) {
              $relObjRepo = $relatedObj->getRepository(true);
              $result = $relObjRepo->find($this->model_data[$attributeName]);
            } 
            
            // Unless we already have an object (maybe it was added to the model from the main app)
            // Then just use what we have
            else {
              $result = $this->model_data[$attributeName];
            }

            // Incase there's loadMap info that needs to be passed in, call populate
            if ($result) {
              $result->_populate([], false, $loadMap);
            }
          }
        }
      }
      return $result;
    }
Uses an attribute's definition to correctly fetch a singular object from a singular relationship.

@param $attributeName string An attribute on the model that is defined as a singular model relationship.
@return model