/** * Loads a mapper class based on the given information. * * @param string $mapperID * @param string $mapperName * @throws PIECE_ORM_ERROR_INVALID_OPERATION * @throws PIECE_ORM_ERROR_NOT_FOUND * @throws PIECE_ORM_ERROR_NOT_READABLE */ function _load($mapperID, $mapperName) { if (Piece_ORM_Mapper_Factory::_loaded($mapperID)) { return; } if (is_null($GLOBALS['PIECE_ORM_Mapper_ConfigDirectory'])) { Piece_ORM_Error::push(PIECE_ORM_ERROR_INVALID_OPERATION, 'The configuration directory must be specified.'); return; } if (!file_exists($GLOBALS['PIECE_ORM_Mapper_ConfigDirectory'])) { Piece_ORM_Error::push(PIECE_ORM_ERROR_NOT_FOUND, "The configuration directory [ {$GLOBALS['PIECE_ORM_Mapper_ConfigDirectory']} ] not found."); return; } if (is_null($GLOBALS['PIECE_ORM_Mapper_CacheDirectory'])) { Piece_ORM_Error::push(PIECE_ORM_ERROR_INVALID_OPERATION, 'The cache directory must be specified.'); return; } if (!file_exists($GLOBALS['PIECE_ORM_Mapper_CacheDirectory'])) { Piece_ORM_Error::push(PIECE_ORM_ERROR_NOT_FOUND, "The cache directory [ {$GLOBALS['PIECE_ORM_Mapper_CacheDirectory']} ] not found."); return; } if (!is_readable($GLOBALS['PIECE_ORM_Mapper_CacheDirectory']) || !is_writable($GLOBALS['PIECE_ORM_Mapper_CacheDirectory'])) { Piece_ORM_Error::push(PIECE_ORM_ERROR_NOT_READABLE, "The cache directory [ {$GLOBALS['PIECE_ORM_Mapper_CacheDirectory']} ] is not readable or writable."); return; } $configFile = "{$GLOBALS['PIECE_ORM_Mapper_ConfigDirectory']}/{$mapperName}.yaml"; if (!file_exists($configFile)) { Piece_ORM_Error::push(PIECE_ORM_ERROR_NOT_FOUND, "The configuration file [ {$configFile} ] not found."); return; } if (!is_readable($configFile)) { Piece_ORM_Error::push(PIECE_ORM_ERROR_NOT_READABLE, "The configuration file [ {$configFile} ] is not readable."); return; } $mapperSource = Piece_ORM_Mapper_Factory::_getMapperSource($mapperID, $mapperName, $configFile); if (Piece_ORM_Error::hasErrors()) { return; } eval($mapperSource); if (!Piece_ORM_Mapper_Factory::_loaded($mapperID)) { Piece_ORM_Error::push(PIECE_ORM_ERROR_NOT_FOUND, "The mapper [ {$mapperName} ] not found."); } }
/** * Sets a database as the current database. * * @param string $database * @throws PIECE_ORM_ERROR_NOT_FOUND */ function setDatabase($database) { if (!$this->_config->checkDatabase($database)) { Piece_ORM_Error::push(PIECE_ORM_ERROR_NOT_FOUND, "The given database [ {$database} ] not found in the current configuration."); return; } $this->_database = $database; $directorySuffix = $this->_config->getDirectorySuffix($this->_database); if (is_null($directorySuffix) || !strlen($directorySuffix)) { Piece_ORM_Mapper_Factory::setConfigDirectory($this->_mapperConfigDirectory); } else { Piece_ORM_Mapper_Factory::setConfigDirectory("{$this->_mapperConfigDirectory}/{$directorySuffix}"); } }
/** * Loads associated objects into appropriate objects. */ function _loadAssociatedObjects() { for ($i = 0, $count = count($this->_relationships); $i < $count; ++$i) { $mapper =& Piece_ORM_Mapper_Factory::factory($this->_relationships[$i]['table']); if (Piece_ORM_Error::hasErrors()) { return; } $this->_associatedObjectLoaders[$this->_relationships[$i]['type']]->loadAll($mapper, $i); if (Piece_ORM_Error::hasErrors()) { return; } } }
/** * Creates an object from the metadata. * * @param string $mapperName * @return stdClass * @throws PIECE_ORM_ERROR_INVALID_OPERATION */ function &createObject($mapperName) { if (!$GLOBALS['PIECE_ORM_Configured']) { Piece_ORM_Error::push(PIECE_ORM_ERROR_INVALID_OPERATION, __FUNCTION__ . ' method must be called after calling configure().'); $return = null; return $return; } $mapper =& Piece_ORM_Mapper_Factory::factory($mapperName); if (Piece_ORM_Error::hasErrors()) { $return = null; return $return; } return $mapper->createObject(); }
/** * Removes associated objects from a table. * * @param array $relationship */ function delete($relationship) { if (!array_key_exists($relationship['mappedAs'], $this->_subject)) { return; } if (!is_null($this->_subject->{$relationship}['mappedAs']) && !is_object($this->_subject->{$relationship}['mappedAs'])) { return; } $mapper =& Piece_ORM_Mapper_Factory::factory($relationship['table']); if (Piece_ORM_Error::hasErrors()) { return; } $mapper->delete($this->_subject->{$relationship}['mappedAs']); }
/** * Removes associated objects from a table. * * @param array $relationship */ function delete($relationship) { $property = Piece_ORM_Inflector::camelize($relationship['through']['referencedColumn'], true); if (!array_key_exists($property, $this->_subject)) { return; } $mapper =& Piece_ORM_Mapper_Factory::factory($relationship['through']['table']); if (Piece_ORM_Error::hasErrors()) { return; } $mapper->executeQuery("DELETE FROM {$relationship['through']['table']} WHERE {$relationship['through']['column']} = " . $mapper->quote($this->_subject->{$property}, $relationship['through']['column']), true); }