ElggEntity::load PHP Method

load() protected method

Loads attributes from the entities table into the object.
protected load ( mixed $guid ) : boolean
$guid mixed GUID of entity or \stdClass object from entities table
return boolean
    protected function load($guid)
    {
        if ($guid instanceof \stdClass) {
            $row = $guid;
        } else {
            $row = get_entity_as_row($guid);
        }
        if ($row) {
            // Create the array if necessary - all subclasses should test before creating
            if (!is_array($this->attributes)) {
                $this->attributes = array();
            }
            // Now put these into the attributes array as core values
            $objarray = (array) $row;
            foreach ($objarray as $key => $value) {
                $this->attributes[$key] = $value;
            }
            // guid needs to be an int  https://github.com/elgg/elgg/issues/4111
            $this->attributes['guid'] = (int) $this->attributes['guid'];
            // for BC with 1.8, ->subtype always returns ID, ->getSubtype() the string
            $this->attributes['subtype'] = (int) $this->attributes['subtype'];
            // Cache object handle
            if ($this->attributes['guid']) {
                _elgg_services()->entityCache->set($this);
            }
            return true;
        }
        return false;
    }

Usage Example

Example #1
0
 /**
  * Override the load function.
  * This function will ensure that all data is loaded (were possible), so
  * if only part of the ElggObject is loaded, it'll load the rest.
  * 
  * @param int $guid
  * @return true|false 
  */
 protected function load($guid)
 {
     // Test to see if we have the generic stuff
     if (!parent::load($guid)) {
         return false;
     }
     // Check the type
     if ($this->attributes['type'] != 'object') {
         throw new InvalidClassException(sprintf(elgg_echo('InvalidClassException:NotValidElggStar'), $guid, get_class()));
     }
     // Load missing data
     $row = get_object_entity_as_row($guid);
     if ($row && !$this->isFullyLoaded()) {
         $this->attributes['tables_loaded']++;
     }
     // If $row isn't a cached copy then increment the counter
     // Now put these into the attributes array as core values
     $objarray = (array) $row;
     foreach ($objarray as $key => $value) {
         $this->attributes[$key] = $value;
     }
     return true;
 }
All Usage Examples Of ElggEntity::load