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