Pimcore\Model\Asset::getById PHP Method

getById() public static method

Static helper to get an asset by the passed ID
public static getById ( integer $id ) : Asset | Archive | Audio | Document | Folder | Image | Text | Unknown | Video
$id integer
return Asset | Archive | Audio | Document | Folder | Image | Text | Unknown | Video
    public static function getById($id)
    {
        $id = intval($id);
        if ($id < 1) {
            return null;
        }
        $cacheKey = "asset_" . $id;
        try {
            $asset = \Zend_Registry::get($cacheKey);
            if (!$asset) {
                throw new \Exception("Asset in registry is null");
            }
        } catch (\Exception $e) {
            try {
                if (!($asset = \Pimcore\Cache::load($cacheKey))) {
                    $asset = new Asset();
                    $asset->getDao()->getById($id);
                    $className = "Pimcore\\Model\\Asset\\" . ucfirst($asset->getType());
                    $asset = \Pimcore::getDiContainer()->make($className);
                    \Zend_Registry::set($cacheKey, $asset);
                    $asset->getDao()->getById($id);
                    \Pimcore\Cache::save($asset, $cacheKey);
                } else {
                    \Zend_Registry::set($cacheKey, $asset);
                }
            } catch (\Exception $e) {
                Logger::warning($e->getMessage());
                return null;
            }
        }
        if (!$asset) {
            return null;
        }
        return $asset;
    }

Usage Example

コード例 #1
0
ファイル: Dao.php プロジェクト: sfie/pimcore
 /**
  * Loads a list of entries for the specicifies parameters, returns an array of Search\Backend\Data
  *
  * @return array
  */
 public function load()
 {
     $entries = array();
     $data = $this->db->fetchAll("SELECT * FROM search_backend_data" . $this->getCondition() . $this->getGroupBy() . $this->getOrder() . $this->getOffsetLimit(), $this->model->getConditionVariables());
     foreach ($data as $entryData) {
         if ($entryData['maintype'] == 'document') {
             $element = Document::getById($entryData['id']);
         } else {
             if ($entryData['maintype'] == 'asset') {
                 $element = Asset::getById($entryData['id']);
             } else {
                 if ($entryData['maintype'] == 'object') {
                     $element = Object::getById($entryData['id']);
                 } else {
                     \Logger::err("unknown maintype ");
                 }
             }
         }
         if ($element) {
             $entry = new Search\Backend\Data();
             $entry->setId(new Search\Backend\Data\Id($element));
             $entry->setFullPath($entryData['fullpath']);
             $entry->setType($entryData['type']);
             $entry->setSubtype($entryData['subtype']);
             $entry->setUserOwner($entryData['userowner']);
             $entry->setUserModification($entryData['usermodification']);
             $entry->setCreationDate($entryData['creationdate']);
             $entry->setModificationDate($entryData['modificationdate']);
             $entry->setPublished($entryData['published'] === 0 ? false : true);
             $entries[] = $entry;
         }
     }
     $this->model->setEntries($entries);
     return $entries;
 }
All Usage Examples Of Pimcore\Model\Asset::getById