]> gitweb.fluxo.info Git - lorea/elgg.git/commitdiff
Fixes #2112 not loading data from entities table twice
authorcash <cash.costello@gmail.com>
Sat, 4 Feb 2012 22:54:54 +0000 (17:54 -0500)
committercash <cash.costello@gmail.com>
Sat, 4 Feb 2012 22:54:54 +0000 (17:54 -0500)
engine/classes/ElggEntity.php
engine/classes/ElggGroup.php
engine/classes/ElggObject.php
engine/classes/ElggSite.php
engine/classes/ElggUser.php

index 2fa8e99393de55524e927480c82c4cebddfe6d12..dc38dafbe76c943b07377c0239f92844f10fcbda 100644 (file)
@@ -1311,12 +1311,16 @@ abstract class ElggEntity extends ElggData implements
        /**
         * Loads attributes from the entities table into the object.
         *
-        * @param int $guid GUID of Entity
+        * @param mixed $guid GUID of entity or stdClass object from entities table
         *
         * @return bool
         */
        protected function load($guid) {
-               $row = get_entity_as_row($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
index ab223e1a4f012dd2109c1e66c7f2ef44698446ee..f7f67bf41a7ef03f3200f467ef4b990f16208182 100644 (file)
@@ -29,12 +29,12 @@ class ElggGroup extends ElggEntity
        }
 
        /**
-        * Construct a new user entity, optionally from a given id value.
+        * Construct a new group entity, optionally from a given guid value.
         *
         * @param mixed $guid If an int, load that GUID.
-        *      If a db row then will attempt to load the rest of the data.
+        *      If an entity table db row, then will load the rest of the data.
         *
-        * @throws Exception if there was a problem creating the user.
+        * @throws Exception if there was a problem creating the group.
         */
        function __construct($guid = null) {
                $this->initializeAttributes();
@@ -43,15 +43,15 @@ class ElggGroup extends ElggEntity
                $this->initialise_attributes(false);
 
                if (!empty($guid)) {
-                       // Is $guid is a DB row - either a entity row, or a user table row.
+                       // Is $guid is a entity table DB row
                        if ($guid instanceof stdClass) {
                                // Load the rest
-                               if (!$this->load($guid->guid)) {
+                               if (!$this->load($guid)) {
                                        $msg = elgg_echo('IOException:FailedToLoadGUID', array(get_class(), $guid->guid));
                                        throw new IOException($msg);
                                }
 
-                               // Is $guid is an ElggGroup? Use a copy constructor
+                       // Is $guid is an ElggGroup? Use a copy constructor
                        } else if ($guid instanceof ElggGroup) {
                                elgg_deprecated_notice('This type of usage of the ElggGroup constructor was deprecated. Please use the clone method.', 1.7);
 
@@ -59,11 +59,11 @@ class ElggGroup extends ElggEntity
                                        $this->attributes[$key] = $value;
                                }
 
-                               // Is this is an ElggEntity but not an ElggGroup = ERROR!
+                       // Is this is an ElggEntity but not an ElggGroup = ERROR!
                        } else if ($guid instanceof ElggEntity) {
                                throw new InvalidParameterException(elgg_echo('InvalidParameterException:NonElggGroup'));
 
-                               // We assume if we have got this far, $guid is an int
+                       // Is it a GUID
                        } else if (is_numeric($guid)) {
                                if (!$this->load($guid)) {
                                        throw new IOException(elgg_echo('IOException:FailedToLoadGUID', array(get_class(), $guid)));
@@ -319,11 +319,9 @@ class ElggGroup extends ElggEntity
        }
 
        /**
-        * Override the load function.
-        * This function will ensure that all data is loaded (were possible), so
-        * if only part of the ElggGroup is loaded, it'll load the rest.
+        * Load the ElggGroup data from the database
         *
-        * @param int $guid GUID of an ElggGroup entity
+        * @param mixed $guid GUID of an ElggGroup entity or database row from entity table
         *
         * @return bool
         */
@@ -333,6 +331,11 @@ class ElggGroup extends ElggEntity
                        return false;
                }
 
+               // Only work with GUID from here
+               if ($guid instanceof stdClass) {
+                       $guid = $guid->guid;
+               }
+
                // Check the type
                if ($this->attributes['type'] != 'group') {
                        $msg = elgg_echo('InvalidClassException:NotValidElggStar', array($guid, get_class()));
index 649e324900841712f714c0819c5846cbef762039..b4bae6825e4140d0fd0a5d4b655ec02fdff1c380 100644 (file)
@@ -41,12 +41,12 @@ class ElggObject extends ElggEntity {
         *
         * If no arguments are passed, create a new entity.
         *
-        * If an argument is passed attempt to load a full Object entity.  Arguments
-        * can be:
+        * If an argument is passed, attempt to load a full ElggObject entity.
+        * Arguments can be:
         *  - The GUID of an object entity.
-        *  - A DB result object with a guid property
+        *  - A DB result object from the entities table with a guid property
         *
-        * @param mixed $guid If an int, load that GUID.  If a db row then will attempt to
+        * @param mixed $guid If an int, load that GUID.  If a db row, then will attempt to
         * load the rest of the data.
         *
         * @throws IOException If passed an incorrect guid
@@ -59,15 +59,15 @@ class ElggObject extends ElggEntity {
                $this->initialise_attributes(false);
 
                if (!empty($guid)) {
-                       // Is $guid is a DB row - either a entity row, or a object table row.
+                       // Is $guid is a DB row from the entity table
                        if ($guid instanceof stdClass) {
                                // Load the rest
-                               if (!$this->load($guid->guid)) {
+                               if (!$this->load($guid)) {
                                        $msg = elgg_echo('IOException:FailedToLoadGUID', array(get_class(), $guid->guid));
                                        throw new IOException($msg);
                                }
 
-                               // Is $guid is an ElggObject? Use a copy constructor
+                       // Is $guid is an ElggObject? Use a copy constructor
                        } else if ($guid instanceof ElggObject) {
                                elgg_deprecated_notice('This type of usage of the ElggObject constructor was deprecated. Please use the clone method.', 1.7);
 
@@ -75,11 +75,11 @@ class ElggObject extends ElggEntity {
                                        $this->attributes[$key] = $value;
                                }
 
-                               // Is this is an ElggEntity but not an ElggObject = ERROR!
+                       // Is this is an ElggEntity but not an ElggObject = ERROR!
                        } else if ($guid instanceof ElggEntity) {
                                throw new InvalidParameterException(elgg_echo('InvalidParameterException:NonElggObject'));
 
-                               // We assume if we have got this far, $guid is an int
+                       // Is it a GUID
                        } else if (is_numeric($guid)) {
                                if (!$this->load($guid)) {
                                        throw new IOException(elgg_echo('IOException:FailedToLoadGUID', array(get_class(), $guid)));
@@ -93,17 +93,22 @@ class ElggObject extends ElggEntity {
        /**
         * Loads the full ElggObject when given a guid.
         *
-        * @param int $guid Guid of an ElggObject
+        * @param mixed $guid GUID of an ElggObject or the stdClass object from entities table
         *
         * @return bool
         * @throws InvalidClassException
         */
        protected function load($guid) {
-               // Test to see if we have the generic stuff
+               // Load data from entity table if needed
                if (!parent::load($guid)) {
                        return false;
                }
 
+               // Only work with GUID from here
+               if ($guid instanceof stdClass) {
+                       $guid = $guid->guid;
+               }
+
                // Check the type
                if ($this->attributes['type'] != 'object') {
                        $msg = elgg_echo('InvalidClassException:NotValidElggStar', array($guid, get_class()));
index b13683a561ae6354a480935a094f7b491ddb9511..49616f1be077fcf6d0e31177fed32882d2d2425e 100644 (file)
@@ -57,8 +57,8 @@ class ElggSite extends ElggEntity {
         *  - A URL as stored in ElggSite->url
         *  - A DB result object with a guid property
         *
-        * @param mixed $guid If an int, load that GUID.  If a db row then will attempt
-        * to load the rest of the data.
+        * @param mixed $guid If an int, load that GUID.  If a db row then will
+        * load the rest of the data.
         *
         * @throws IOException If passed an incorrect guid
         * @throws InvalidParameterException If passed an Elgg* Entity that isn't an ElggSite
@@ -70,15 +70,15 @@ class ElggSite extends ElggEntity {
                $this->initialise_attributes(false);
 
                if (!empty($guid)) {
-                       // Is $guid is a DB row - either a entity row, or a site table row.
+                       // Is $guid is a DB entity table row
                        if ($guid instanceof stdClass) {
                                // Load the rest
-                               if (!$this->load($guid->guid)) {
+                               if (!$this->load($guid)) {
                                        $msg = elgg_echo('IOException:FailedToLoadGUID', array(get_class(), $guid->guid));
                                        throw new IOException($msg);
                                }
 
-                               // Is $guid is an ElggSite? Use a copy constructor
+                       // Is $guid is an ElggSite? Use a copy constructor
                        } else if ($guid instanceof ElggSite) {
                                elgg_deprecated_notice('This type of usage of the ElggSite constructor was deprecated. Please use the clone method.', 1.7);
 
@@ -86,18 +86,18 @@ class ElggSite extends ElggEntity {
                                        $this->attributes[$key] = $value;
                                }
 
-                               // Is this is an ElggEntity but not an ElggSite = ERROR!
+                       // Is this is an ElggEntity but not an ElggSite = ERROR!
                        } else if ($guid instanceof ElggEntity) {
                                throw new InvalidParameterException(elgg_echo('InvalidParameterException:NonElggSite'));
 
-                               // See if this is a URL
+                       // See if this is a URL
                        } else if (strpos($guid, "http") !== false) {
                                $guid = get_site_by_url($guid);
                                foreach ($guid->attributes as $key => $value) {
                                        $this->attributes[$key] = $value;
                                }
 
-                               // We assume if we have got this far, $guid is an int
+                       // Is it a GUID
                        } else if (is_numeric($guid)) {
                                if (!$this->load($guid)) {
                                        throw new IOException(elgg_echo('IOException:FailedToLoadGUID', array(get_class(), $guid)));
@@ -111,7 +111,7 @@ class ElggSite extends ElggEntity {
        /**
         * Loads the full ElggSite when given a guid.
         *
-        * @param int $guid Guid of ElggSite entity
+        * @param mixed $guid GUID of ElggSite entity or database row object
         *
         * @return bool
         * @throws InvalidClassException
@@ -122,6 +122,11 @@ class ElggSite extends ElggEntity {
                        return false;
                }
 
+               // Only work with GUID from here
+               if ($guid instanceof stdClass) {
+                       $guid = $guid->guid;
+               }
+
                // Check the type
                if ($this->attributes['type'] != 'site') {
                        $msg = elgg_echo('InvalidClassException:NotValidElggStar', array($guid, get_class()));
index e9cbc6cb2f6b19346da9b8e4a9662b7bad1fe61f..d7bb89265106b1189a186b1c006592150929219f 100644 (file)
@@ -47,7 +47,7 @@ class ElggUser extends ElggEntity
         * Construct a new user entity, optionally from a given id value.
         *
         * @param mixed $guid If an int, load that GUID.
-        *      If a db row then will attempt to load the rest of the data.
+        *      If an entity table db row then will load the rest of the data.
         *
         * @throws Exception if there was a problem creating the user.
         */
@@ -58,15 +58,15 @@ class ElggUser extends ElggEntity
                $this->initialise_attributes(false);
 
                if (!empty($guid)) {
-                       // Is $guid is a DB row - either a entity row, or a user table row.
+                       // Is $guid is a DB entity row
                        if ($guid instanceof stdClass) {
                                // Load the rest
-                               if (!$this->load($guid->guid)) {
+                               if (!$this->load($guid)) {
                                        $msg = elgg_echo('IOException:FailedToLoadGUID', array(get_class(), $guid->guid));
                                        throw new IOException($msg);
                                }
 
-                               // See if this is a username
+                       // See if this is a username
                        } else if (is_string($guid)) {
                                $user = get_user_by_username($guid);
                                if ($user) {
@@ -75,7 +75,7 @@ class ElggUser extends ElggEntity
                                        }
                                }
 
-                               // Is $guid is an ElggUser? Use a copy constructor
+                       // Is $guid is an ElggUser? Use a copy constructor
                        } else if ($guid instanceof ElggUser) {
                                elgg_deprecated_notice('This type of usage of the ElggUser constructor was deprecated. Please use the clone method.', 1.7);
 
@@ -83,11 +83,11 @@ class ElggUser extends ElggEntity
                                        $this->attributes[$key] = $value;
                                }
 
-                               // Is this is an ElggEntity but not an ElggUser = ERROR!
+                       // Is this is an ElggEntity but not an ElggUser = ERROR!
                        } else if ($guid instanceof ElggEntity) {
                                throw new InvalidParameterException(elgg_echo('InvalidParameterException:NonElggUser'));
 
-                               // We assume if we have got this far, $guid is an int
+                       // Is it a GUID
                        } else if (is_numeric($guid)) {
                                if (!$this->load($guid)) {
                                        throw new IOException(elgg_echo('IOException:FailedToLoadGUID', array(get_class(), $guid)));
@@ -99,11 +99,9 @@ class ElggUser extends ElggEntity
        }
 
        /**
-        * Override the load function.
-        * This function will ensure that all data is loaded (were possible), so
-        * if only part of the ElggUser is loaded, it'll load the rest.
+        * Load the ElggUser data from the database
         *
-        * @param int $guid ElggUser GUID
+        * @param mixed $guid ElggUser GUID or stdClass database row from entity table
         *
         * @return bool
         */
@@ -113,6 +111,11 @@ class ElggUser extends ElggEntity
                        return false;
                }
 
+               // Only work with GUID from here
+               if ($guid instanceof stdClass) {
+                       $guid = $guid->guid;
+               }
+
                // Check the type
                if ($this->attributes['type'] != 'user') {
                        $msg = elgg_echo('InvalidClassException:NotValidElggStar', array($guid, get_class()));