/** * Custom instance loader for collections that respects policy checking. * * {@inheritdoc} */ protected static function _loadCollectionInstance(xPDO &$xpdo, array &$objCollection, $className, $criteria, $row, $fromCache, $cacheFlag = true) { $loaded = false; if ($obj = xPDOObject::_loadInstance($xpdo, $className, $criteria, $row)) { if (($cacheKey = $obj->getPrimaryKey()) && !$obj->isLazy()) { if (is_array($cacheKey)) { $pkval = implode('-', $cacheKey); } else { $pkval = $cacheKey; } if ($obj->checkPolicy('load')) { if ($xpdo->getOption(xPDO::OPT_CACHE_DB_COLLECTIONS, array(), 1) == 2 && $xpdo->_cacheEnabled && $cacheFlag) { if (!$fromCache) { $pkCriteria = $xpdo->newQuery($className, $cacheKey, $cacheFlag); $xpdo->toCache($pkCriteria, $obj, $cacheFlag); } else { $obj->_cacheFlag = true; } } $objCollection[$pkval] = $obj; $loaded = true; } } else { if ($obj->checkPolicy('load')) { $objCollection[] = $obj; $loaded = true; } } } return $loaded; }
/** * Custom instance from row loader that respects policy checking * * {@inheritdoc} */ public static function _loadInstance(& $xpdo, $className, $criteria, $row) { $instance = xPDOObject :: _loadInstance($xpdo, $className, $criteria, $row); if ($instance instanceof modAccessibleObject && !$instance->checkPolicy('load')) { if ($xpdo instanceof modX) { $userid = $xpdo->getLoginUserID(); if (!$userid) $userid = '0'; $xpdo->log(xPDO::LOG_LEVEL_INFO, "Principal {$userid} does not have permission to load object of class {$instance->_class} with primary key: " . (is_object($instance) && method_exists($instance,'getPrimaryKey') ? print_r($instance->getPrimaryKey(), true) : '')); } $instance = null; } return $instance; }
/** * Load an instance of an xPDOObject or derivative class. * * @static * @param xPDO &$xpdo A valid xPDO instance. * @param string $className Name of the class. * @param mixed $criteria A valid primary key, criteria array, or * xPDOCriteria instance. * @param boolean|integer $cacheFlag Indicates if the objects should be * cached and optionally, by specifying an integer value, for how many * seconds. * @return object|null An instance of the requested class, or null if it * could not be instantiated. */ 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)) { $criteria = $xpdo->addDerivativeCriteria($className, $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 = xPDOObject::_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; }