Beispiel #1
0
 /**
  * Normalizes a relationship definition.
  *
  * @return array
  * @throws PIECE_ORM_ERROR_INVALID_CONFIGURATION
  * @throws PIECE_ORM_ERROR_NOT_FOUND
  */
 function normalize()
 {
     if (!array_key_exists('table', $this->_relationship)) {
         Piece_ORM_Error::push(PIECE_ORM_ERROR_INVALID_CONFIGURATION, 'The element [ table ] is required to generate a relationship property declaration.');
         return;
     }
     if (!array_key_exists('mappedAs', $this->_relationship)) {
         Piece_ORM_Error::push(PIECE_ORM_ERROR_INVALID_CONFIGURATION, 'The element [ mappedAs ] is required to generate a relationship property declaration.');
         return;
     }
     $this->_relationshipMetadata =& Piece_ORM_Metadata_Factory::factory($this->_relationship['table']);
     if (Piece_ORM_Error::hasErrors()) {
         return;
     }
     if ($this->_checkHavingSinglePrimaryKey()) {
         if (!$this->_relationshipMetadata->getPrimaryKey()) {
             Piece_ORM_Error::push(PIECE_ORM_ERROR_NOT_FOUND, 'A single primary key field is required in the table [ ' . $this->_relationshipMetadata->getTableName(true) . ' ].');
             return;
         }
     }
     if (!array_key_exists('column', $this->_relationship)) {
         if (!$this->_normalizeColumn()) {
             Piece_ORM_Error::push(PIECE_ORM_ERROR_INVALID_CONFIGURATION, 'A single primary key field is required, if the element [ column ] in the element [ relationship ] omit.');
             return;
         }
     }
     if (!$this->_relationshipMetadata->hasField($this->_relationship['column'])) {
         Piece_ORM_Error::push(PIECE_ORM_ERROR_INVALID_CONFIGURATION, "The field [ {$this->_relationship['column']} ] not found in the table [ " . $this->_relationshipMetadata->getTableName(true) . ' ].');
         return;
     }
     if (!array_key_exists('referencedColumn', $this->_relationship)) {
         if (!$this->_normalizeReferencedColumn()) {
             Piece_ORM_Error::push(PIECE_ORM_ERROR_INVALID_CONFIGURATION, 'A single primary key field is required, if the element [ referencedColumn ] in the element [ relationship ] omit.');
             return;
         }
     }
     if ($this->_referencedColumnRequired && !$this->_metadata->hasField($this->_relationship['referencedColumn'])) {
         Piece_ORM_Error::push(PIECE_ORM_ERROR_INVALID_CONFIGURATION, "The field [ {$this->_relationship['referencedColumn']} ] not found in the table [ " . $this->_metadata->getTableName(true) . ' ].');
         return;
     }
     if (!array_key_exists('orderBy', $this->_relationship)) {
         $this->_relationship['orderBy'] = null;
     }
     $this->_normalizeOrderBy();
     $this->_normalizeThrough();
     if (Piece_ORM_Error::hasErrors()) {
         return;
     }
     return $this->_relationship;
 }
Beispiel #2
0
 /**
  * Normalizes "through" definition.
  *
  * @throws PIECE_ORM_ERROR_INVALID_CONFIGURATION
  */
 function _normalizeThrough()
 {
     if (!array_key_exists('through', $this->_relationship)) {
         $this->_relationship['through'] = array();
     }
     if (!array_key_exists('table', $this->_relationship['through'])) {
         $throughTableName1 = $this->_metadata->getTableName(true) . "_{$this->_relationship['table']}";
         $throughTableName2 = "{$this->_relationship['table']}_" . $this->_metadata->getTableName(true);
         foreach (array($throughTableName1, $throughTableName2) as $throughTableName) {
             Piece_ORM_Error::disableCallback();
             $throughMetadata =& Piece_ORM_Metadata_Factory::factory($throughTableName);
             Piece_ORM_Error::enableCallback();
             if (Piece_ORM_Error::hasErrors()) {
                 $error = Piece_ORM_Error::pop();
                 if ($error['code'] != PIECE_ORM_ERROR_NOT_FOUND) {
                     Piece_ORM_Error::push($error['code'], $error['message'], 'exception', array(), $error);
                     return;
                 }
                 continue;
             }
             $this->_relationship['through']['table'] = $throughTableName;
             break;
         }
         if (!$throughMetadata) {
             Piece_ORM_Error::push(PIECE_ORM_ERROR_INVALID_CONFIGURATION, "One of [ {$throughTableName1} ] or [ {$throughTableName2} ] must exists in the database, if the element [ table ] in the element [ through ] omit.");
             return;
         }
     }
     $throughMetadata =& Piece_ORM_Metadata_Factory::factory($this->_relationship['through']['table']);
     if (Piece_ORM_Error::hasErrors()) {
         return;
     }
     if (!array_key_exists('column', $this->_relationship['through'])) {
         if ($primaryKey = $this->_metadata->getPrimaryKey()) {
             $this->_relationship['through']['column'] = $this->_metadata->getTableName(true) . "_{$primaryKey}";
         } else {
             Piece_ORM_Error::push(PIECE_ORM_ERROR_INVALID_CONFIGURATION, 'A single primary key field is required, if the element [ column ] in the element [ through ] omit.');
             return;
         }
     }
     if (!$throughMetadata->hasField($this->_relationship['through']['column'])) {
         Piece_ORM_Error::push(PIECE_ORM_ERROR_INVALID_CONFIGURATION, "The field [ {$this->_relationship['through']['column']} ] not found in the table [ " . $throughMetadata->getTableName(true) . ' ].');
         return;
     }
     if (!array_key_exists('referencedColumn', $this->_relationship['through'])) {
         if ($primaryKey = $this->_metadata->getPrimaryKey()) {
             $this->_relationship['through']['referencedColumn'] = $primaryKey;
         } else {
             Piece_ORM_Error::push(PIECE_ORM_ERROR_INVALID_CONFIGURATION, 'A single primary key field is required, if the element [ referencedColumn ] in the element [ through ] omit.');
             return;
         }
     }
     if (!$this->_metadata->hasField($this->_relationship['through']['referencedColumn'])) {
         Piece_ORM_Error::push(PIECE_ORM_ERROR_INVALID_CONFIGURATION, "The field [ {$this->_relationship['through']['referencedColumn']} ] not found in the table [ " . $this->_metadata->getTableName(true) . ' ].');
         return;
     }
     if (!array_key_exists('inverseColumn', $this->_relationship['through'])) {
         if ($primaryKey = $this->_relationshipMetadata->getPrimaryKey()) {
             $this->_relationship['through']['inverseColumn'] = $this->_relationshipMetadata->getTableName(true) . "_{$primaryKey}";
         } else {
             Piece_ORM_Error::push(PIECE_ORM_ERROR_INVALID_CONFIGURATION, 'A single primary key field is required, if the element [ column ] in the element [ through ] omit.');
             return;
         }
     }
     if (!$throughMetadata->hasField($this->_relationship['through']['inverseColumn'])) {
         Piece_ORM_Error::push(PIECE_ORM_ERROR_INVALID_CONFIGURATION, "The field [ {$this->_relationship['through']['inverseColumn']} ] not found in the table [ " . $throughMetadata->getTableName(true) . ' ].');
         return;
     }
 }
Beispiel #3
0
 /**
  * Creates a Piece_ORM_Metadata object from a cache or a database.
  *
  * @param string $tableName
  * @param string $tableID
  * @return Piece_ORM_Metadata
  */
 function &_createMetadata($tableName, $tableID)
 {
     if (!file_exists($GLOBALS['PIECE_ORM_Metadata_CacheDirectory'])) {
         trigger_error("The cache directory [ {$GLOBALS['PIECE_ORM_Metadata_CacheDirectory']} ] is not found.", E_USER_WARNING);
         return Piece_ORM_Metadata_Factory::_createMetadataFromDatabase($tableName);
     }
     if (!is_readable($GLOBALS['PIECE_ORM_Metadata_CacheDirectory']) || !is_writable($GLOBALS['PIECE_ORM_Metadata_CacheDirectory'])) {
         trigger_error("The cache directory [ {$GLOBALS['PIECE_ORM_Metadata_CacheDirectory']} ] is not readable or writable.", E_USER_WARNING);
         return Piece_ORM_Metadata_Factory::_createMetadataFromDatabase($tableName);
     }
     return Piece_ORM_Metadata_Factory::_getMetadata($tableName, $tableID);
 }
Beispiel #4
0
 /**
  * Generates a mapper source from the given configuration file.
  *
  * @param string $mapperID
  * @param string $mapperName
  * @param string $configFile
  * @return string
  */
 function _generateMapperSource($mapperID, $mapperName, $configFile)
 {
     $metadata =& Piece_ORM_Metadata_Factory::factory($mapperName);
     if (Piece_ORM_Error::hasErrors()) {
         return;
     }
     $generator =& new Piece_ORM_Mapper_Generator(Piece_ORM_Mapper_Factory::_getMapperClass($mapperID), $mapperName, Spyc::YAMLLoad($configFile), $metadata, get_class_methods('Piece_ORM_Mapper_Common'));
     return $generator->generate();
 }
Beispiel #5
0
 /**
  * Configures the Piece_ORM environment.
  *
  * First this method tries to load a configuration from a configuration
  * file in the given configration directory using
  * Piece_ORM_Config_Factory::factory(). The method creates a new object
  * if the load failed.
  * Second this method sets the configuration to the current context.
  * And also this method sets the configuration directory for the mapper
  * configuration, and the cache directory for mappers, and the cache
  * directory for Piece_ORM_Metadata class.
  *
  * @param string $configDirectory
  * @param string $cacheDirectory
  * @param string $mapperConfigDirectory
  */
 function configure($configDirectory, $cacheDirectory, $mapperConfigDirectory)
 {
     $config =& Piece_ORM_Config_Factory::factory($configDirectory, $cacheDirectory);
     $context =& Piece_ORM_Context::singleton();
     $context->setConfiguration($config);
     $context->setMapperConfigDirectory($mapperConfigDirectory);
     $defaultDatabase = $config->getDefaultDatabase();
     if (!is_null($defaultDatabase)) {
         $context->setDatabase($defaultDatabase);
     }
     Piece_ORM_Mapper_Factory::setCacheDirectory($cacheDirectory);
     Piece_ORM_Metadata_Factory::setCacheDirectory($cacheDirectory);
     $GLOBALS['PIECE_ORM_Configured'] = true;
 }