Beispiel #1
0
 /**
  * Loads data into the object from the database, searching it by id
  *
  * @param $id
  * @param \DateTime $revisionDate
  *
  * @throws \Exception
  * @return Model
  */
 public function load($id, \DateTime $revisionDate = null)
 {
     if (!is_null($revisionDate) && $this->isTemporal()) {
         throw new \Exception(__METHOD__ . ' ' . ' @TODO implement fetching by revision');
     } else {
         if ($this->isCached && Util::lavnn('id', $this->data, 0) != 0) {
             // nothing to do, data is already loaded
         } else {
             // first, to ask from the cache
             $cached = $this->cache->getModelData($this->tableName, $this->getID());
             if (!$cached) {
                 // load from the database
                 $sqlFileName = get_class($this) . '_GetDetails';
                 if (Locator::moduleFileExists($this->moduleName, "sql/{$sqlFileName}.sql")) {
                     // tries to get details about the object using custom query, written for this model specifically
                     $query = TextProcessor::doSqlTemplate($this->moduleName, $sqlFileName, array('id' => $id));
                 } else {
                     // uses standard query that is valid for any model
                     $sqlParams = array('tableName' => $this->tableName, 'id' => $id);
                     $query = TextProcessor::doText(file_get_contents(__DIR__ . "/Templates/GetDetails.sql"), $sqlParams);
                 }
                 $this->data = Database::getInstance()->getRow($query);
                 // IMPORTANT do not save model data to cache at this stage by default.
                 //  If done so, subclasses would miss info set in getData(). We require explicit cacheOnLoad = true for this
                 if ($this->cacheOnLoad && !is_null($this->cache)) {
                     $this->cache->setModelData($this);
                     $this->isCached = true;
                 }
             } else {
                 // use cached version
                 $this->data = $cached;
                 $this->isCached = true;
             }
         }
     }
     return $this;
 }