Elgg\Database\EntityTable::get PHP Method

get() public method

Loads and returns an entity object from a guid.
public get ( integer $guid, string $type = '' ) : ElggEntit\ElggEntity | stdClas\stdClass | false
$guid integer The GUID of the entity
$type string The type of the entity. If given, even an existing entity with the given GUID will not be returned unless its type matches.
return ElggEntit\ElggEntity | stdClas\stdClass | false The correct Elgg or custom object based upon entity type and subtype
    public function get($guid, $type = '')
    {
        // We could also use: if (!(int) $guid) { return false },
        // but that evaluates to a false positive for $guid = true.
        // This is a bit slower, but more thorough.
        if (!is_numeric($guid) || $guid === 0 || $guid === '0') {
            return false;
        }
        $guid = (int) $guid;
        $entity = $this->getFromCache($guid);
        if ($entity && (!$type || elgg_instanceof($entity, $type))) {
            return $entity;
        }
        $row = $this->getRow($guid);
        if (!$row) {
            return false;
        }
        if ($type && $row->type != $type) {
            return false;
        }
        $entity = $this->rowToElggStar($row);
        if ($entity instanceof ElggEntity) {
            $entity->storeInPersistedCache(_elgg_get_memcache('new_entity_cache'));
        }
        return $entity;
    }

Usage Example

コード例 #1
0
ファイル: UserCapabilities.php プロジェクト: elgg/elgg
 /**
  * Can a user edit this entity?
  *
  * @tip Can be overridden by registering for the permissions_check plugin hook.
  *
  * @param ElggEntity $entity    Object entity
  * @param int        $user_guid The user GUID, optionally (default: logged in user)
  *
  * @return bool Whether this entity is editable by the given user.
  * @see elgg_set_ignore_access()
  */
 public function canEdit(ElggEntity $entity, $user_guid = 0)
 {
     try {
         $user = $this->entities->getUserForPermissionsCheck($user_guid);
     } catch (UserFetchFailureException $e) {
         return false;
     }
     // Test user if possible - should default to false unless a plugin hook says otherwise
     $default = call_user_func(function () use($entity, $user) {
         if (!$user) {
             return false;
         }
         // favor the persisted attributes if not saved
         $attrs = array_merge(['owner_guid' => $entity->owner_guid, 'container_guid' => $entity->container_guid], $entity->getOriginalAttributes());
         if ($attrs['owner_guid'] == $user->guid) {
             return true;
         }
         if ($attrs['container_guid'] == $user->guid) {
             return true;
         }
         if ($entity->guid == $user->guid) {
             return true;
         }
         $container = $this->entities->get($attrs['container_guid']);
         return $container && $container->canEdit($user->guid);
     });
     $params = array('entity' => $entity, 'user' => $user);
     return $this->hooks->trigger('permissions_check', $entity->getType(), $params, $default);
 }
All Usage Examples Of Elgg\Database\EntityTable::get