diff options
author | Brett Profitt <brett.profitt@gmail.com> | 2012-05-14 23:41:31 -0700 |
---|---|---|
committer | Brett Profitt <brett.profitt@gmail.com> | 2012-05-14 23:41:31 -0700 |
commit | 40d35166d3f2211ab76943834a983330413ab761 (patch) | |
tree | 14a505037c0d25ea931b97cdac8668b8fa5d3403 /engine/classes | |
parent | 11e83f1864a17a98c794dd598731a5129aa8a8e7 (diff) | |
download | elgg-40d35166d3f2211ab76943834a983330413ab761.tar.gz elgg-40d35166d3f2211ab76943834a983330413ab761.tar.bz2 |
Refs #4313. Loading ElggPlugin classes with the fully joined objects table.
This cuts the number of db queries in half for loading plugins with elgg_get_plugins().
Will look to adapt these techniques to other classes in 1.8.6.
Diffstat (limited to 'engine/classes')
-rw-r--r-- | engine/classes/ElggPlugin.php | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/engine/classes/ElggPlugin.php b/engine/classes/ElggPlugin.php index 33f14ae37..8c9093834 100644 --- a/engine/classes/ElggPlugin.php +++ b/engine/classes/ElggPlugin.php @@ -79,6 +79,68 @@ class ElggPlugin extends ElggObject { } /** + * Overridden from ElggEntity and ElggObject::load(). Core always inits plugins with + * a query joined to the objects_entity table, so all the info is there. + * + * @param mixed $guid GUID of an ElggObject or the stdClass object from entities table + * + * @return bool + * @throws InvalidClassException + */ + protected function load($guid) { + + $expected_attributes = $this->attributes; + unset($expected_attributes['tables_split']); + unset($expected_attributes['tables_loaded']); + + // this was loaded with a full join + $needs_loaded = false; + + if ($guid instanceof stdClass) { + $row = (array) $guid; + $missing_attributes = array_diff_key($expected_attributes, $row); + if ($missing_attributes) { + $needs_loaded = true; + $old_guid = $guid; + $guid = $row['guid']; + } else { + $this->attributes = $row; + } + } else { + $needs_loaded = true; + } + + if ($needs_loaded) { + $entity = (array) get_entity_as_row($guid); + $object = (array) get_object_entity_as_row($guid); + + if (!$entity || !$object) { + return false; + } + + $this->attributes = array_merge($this->attributes, $entity, $object); + } + + $this->attributes['tables_loaded'] = 2; + + // Check the type + if ($this->attributes['type'] != 'object') { + $msg = elgg_echo('InvalidClassException:NotValidElggStar', array($guid, get_class())); + throw new InvalidClassException($msg); + } + + // guid needs to be an int http://trac.elgg.org/ticket/4111 + $this->attributes['guid'] = (int)$this->attributes['guid']; + + // cache the entity + if ($this->attributes['guid']) { + cache_entity($this); + } + + return true; + } + + /** * Save the plugin object. Make sure required values exist. * * @see ElggObject::save() |