Pimcore\Model\Document::getById PHP Method

getById() public static method

Static helper to get a Document by it's ID
public static getById ( integer $id ) : Document | Email | Folder | Hardlink | Link | Page | Printcontainer | Printpage | Snippet | Newsletter
$id integer
return Document | Email | Folder | Hardlink | Link | Page | Printcontainer | Printpage | Snippet | Newsletter
    public static function getById($id)
    {
        $id = intval($id);
        if ($id < 1) {
            return null;
        }
        $cacheKey = "document_" . $id;
        try {
            $document = \Zend_Registry::get($cacheKey);
            if (!$document) {
                throw new \Exception("Document in registry is null");
            }
        } catch (\Exception $e) {
            try {
                if (!($document = \Pimcore\Cache::load($cacheKey))) {
                    $document = new Document();
                    $document->getDao()->getById($id);
                    $className = "Pimcore\\Model\\Document\\" . ucfirst($document->getType());
                    // this is the fallback for custom document types using prefixes
                    // so we need to check if the class exists first
                    if (!Tool::classExists($className)) {
                        $oldStyleClass = "Document_" . ucfirst($document->getType());
                        if (Tool::classExists($oldStyleClass)) {
                            $className = $oldStyleClass;
                        }
                    }
                    $document = \Pimcore::getDiContainer()->make($className);
                    \Zend_Registry::set($cacheKey, $document);
                    $document->getDao()->getById($id);
                    \Pimcore\Cache::save($document, $cacheKey);
                } else {
                    \Zend_Registry::set($cacheKey, $document);
                }
            } catch (\Exception $e) {
                Logger::warning($e->getMessage());
                return null;
            }
        }
        if (!$document) {
            return null;
        }
        return $document;
    }

Usage Example

Example #1
0
 /**
  * @param $document
  * @param null $language
  * @return Document|null
  */
 public function inotherlang($document, $language = null)
 {
     $documentInOtherLang = null;
     if (is_null($language)) {
         $language = CURRENT_LANGUAGE;
     }
     if ($document instanceof Document) {
         $id = $document->getId();
     } elseif (is_numeric($document)) {
         $id = $document;
     } else {
         $id = 0;
     }
     $otherLangId = null;
     try {
         if (class_exists('\\Multilingual\\Document')) {
             $otherLangId = \Multilingual\Document::getDocumentIdInOtherLanguage($id, $language);
         } else {
             $otherLangId = $id;
         }
     } catch (Exception $e) {
     }
     if ($otherLangId) {
         $documentInOtherLang = Document::getById($otherLangId);
     }
     return $documentInOtherLang;
 }
All Usage Examples Of Pimcore\Model\Document::getById