/** * 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; } }
/** * 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; }
/** * 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(); }