コード例 #1
0
 /**
  * Custom instance loader that forces access policy checking.
  *
  * {@inheritdoc}
  */
 public static function load(xPDO & $xpdo, $className, $criteria, $cacheFlag= true) {
     $instance= null;
     $fromCache= false;
     if ($className= $xpdo->loadClass($className)) {
         if (!is_object($criteria)) {
             $criteria= $xpdo->getCriteria($className, $criteria, $cacheFlag);
         }
         if (is_object($criteria)) {
             $row= null;
             if ($xpdo->_cacheEnabled && $criteria->cacheFlag && $cacheFlag) {
                 $row= $xpdo->fromCache($criteria, $className);
             }
             if ($row === null || !is_array($row)) {
                 if ($rows= xPDOObject :: _loadRows($xpdo, $className, $criteria)) {
                     $row= $rows->fetch(PDO::FETCH_ASSOC);
                     $rows->closeCursor();
                 }
             } else {
                 $fromCache= true;
             }
             if (!is_array($row)) {
                 if ($xpdo->getDebug() === true) $xpdo->log(xPDO::LOG_LEVEL_DEBUG, "Fetched empty result set from statement: " . print_r($criteria->sql, true) . " with bindings: " . print_r($criteria->bindings, true));
             } else {
                 $instance= modAccessibleObject :: _loadInstance($xpdo, $className, $criteria, $row);
                 if (is_object($instance)) {
                     if (!$fromCache && $cacheFlag && $xpdo->_cacheEnabled) {
                         $xpdo->toCache($criteria, $instance, $cacheFlag);
                         if ($xpdo->getOption(xPDO::OPT_CACHE_DB_OBJECTS_BY_PK) && ($cacheKey= $instance->getPrimaryKey()) && !$instance->isLazy()) {
                             $pkCriteria = $xpdo->newQuery($className, $cacheKey, $cacheFlag);
                             $xpdo->toCache($pkCriteria, $instance, $cacheFlag);
                         }
                     }
                     if ($xpdo->getDebug() === true) $xpdo->log(xPDO::LOG_LEVEL_DEBUG, "Loaded object instance: " . print_r($instance->toArray('', true), true));
                 }
             }
         } else {
             $xpdo->log(xPDO::LOG_LEVEL_ERROR, 'No valid statement could be found in or generated from the given criteria.');
         }
     } else {
         $xpdo->log(xPDO::LOG_LEVEL_ERROR, 'Invalid class specified: ' . $className);
     }
     return $instance;
 }