 +
 +        $orderBySQL = ';';
 +        $fieldNameSQL = '';
-+        $joinSQL = '';
-+        // If the fieldName is set, also add the fieldName SQL, Join SQL and orderBy SQL
-+        if ($fieldName) {
-+            $fieldNameSQL = self::$prefix .'metadata.key = \''. $fieldName .'\' AND '. self::$prefix .'metadata.value not like \'\'';
-+            $joinSQL = 'LEFT JOIN '. self::$prefix .'metadata ON '. self::$prefix .'entity.eid = '. self::$prefix .'metadata.eid';
-+            $orderBySQL = 'ORDER BY '. self::$prefix .'metadata.value ASC;';
-+        }
 +
 +        $excludeSQL = '';
          if (!is_null($state_exclude)) {
 -            $excludeSQL = ' AND `eid` NOT IN (SELECT DISTINCT `eid` FROM janus__entity WHERE `state` IN (\'' . $state_exclude . '\'));';
-+            $excludeSQL = ' AND '. self::$prefix .'entity.eid NOT IN (SELECT DISTINCT `eid` FROM janus__entity WHERE `state` IN (\'' . $state_exclude . '\'))';
++            $excludeSQL = ' AND '. self::$prefix .'entity.eid NOT IN (SELECT DISTINCT `eid` FROM '. self::$prefix .'entity WHERE `state` IN (\'' . $state_exclude . '\'))';
          }
  
          $guard = new sspmod_janus_UIguard($this->_config->getArray('access', array()));
  
          if($guard->hasPermission('allentities', null, $this->_user->getType(), TRUE)) {
++            
++            $joinSQL = '';
++            // If the fieldName is set, also add the fieldName SQL, Join SQL and orderBy SQL
++            if ($fieldName) {
++                $fieldNameSQL = self::$prefix .'metadata.key = \''. $fieldName .'\' AND '. self::$prefix .'metadata.value not like \'\'';
++                $joinSQL = ' LEFT JOIN '. self::$prefix .'metadata ON '. self::$prefix .'entity.eid = '. self::$prefix .'metadata.eid';
++                $orderBySQL = ' ORDER BY '. self::$prefix .'metadata.value ASC;';
++            }
++
              if(!is_null($state)) {
 -                $st = $this->execute('SELECT DISTINCT `eid` FROM '. self::$prefix .'entity WHERE `state` = ?' . $excludeSQL,
 -                    array($state)                     

### Eclipse Workspace Patch 1.0
#P serviceregistry
Index: modules/janus/lib/UserController.php
===================================================================
--- modules/janus/lib/UserController.php	(revision 680)
+++ modules/janus/lib/UserController.php	(working copy)
@@ -104,27 +104,56 @@
      */
     private function _loadEntities($state = null, $state_exclude = null)
     {

             if ($st === false) {
                 return false;
             }
-
         } else {
+            if ($fieldName) {
+                $fieldNameSQL = ' AND jm.key = \''. $fieldName .'\' AND jm.value not like \'\'';
+                $orderBySQL = ' ORDER BY jm.value ASC;';
+            }
             if(!is_null($state)) {
                 $st = $this->execute(
-                    'SELECT * 
-                    FROM '. self::$prefix .'hasEntity t1, '. self::$prefix .'entity t2 
-                    WHERE t1.`uid` = ? ANd t1.eid = t2.eid AND t2.state = ?' . $excludeSQL,
+                    'SELECT DISTINCT jhe.eid 
+                    FROM '. self::$prefix .'hasEntity jhe, '. self::$prefix .'entity je, '. self::$prefix .'metadata jm 
+                    WHERE jhe.`uid` = ? AND jhe.eid = je.eid AND jhe.eid = jm.eid AND je.state = ?'. $fieldNameSQL . $excludeSQL . $orderBySQL,
                     array($this->_user->getUid(), $state)
                 );
             } else {
                 $st = $this->execute(
-                    'SELECT * 
-                    FROM '. self::$prefix .'hasEntity 
-                    WHERE `uid` = ?' . $excludeSQL,
+                    'SELECT DISTINCT jhe.eid 
+                    FROM '. self::$prefix .'hasEntity jhe, '. self::$prefix .'metadata jm
+                    WHERE jhe.uid = ? AND jhe.eid = jm.eid '. $fieldNameSQL  . $excludeSQL . $orderBySQL,
                     array($this->_user->getUid())
                 );
             }
