. */ /** * RangeVariableDeclaration = identifier {"." identifier} [["AS"] IdentificationVariable] * * @package Doctrine * @subpackage Query * @author Guilherme Blanco * @author Janne Vanhala * @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @link http://www.phpdoctrine.org * @since 2.0 * @version $Revision$ */ class Doctrine_Query_Production_RangeVariableDeclaration extends Doctrine_Query_Production { protected $_identifiers = array(); protected $_queryComponent; protected $_identificationVariable; public function syntax($paramHolder) { // RangeVariableDeclaration = identifier {"." identifier} [["AS"] IdentificationVariable] $this->_parser->match(Doctrine_Query_Token::T_IDENTIFIER); $this->_identifiers[] = $this->_parser->token['value']; while ($this->_isNextToken('.')) { $this->_parser->match('.'); $this->_parser->match(Doctrine_Query_Token::T_IDENTIFIER); $this->_identifiers[] = $this->_parser->token['value']; } if ($this->_isNextToken(Doctrine_Query_Token::T_AS)) { $this->_parser->match(Doctrine_Query_Token::T_AS); } if ($this->_isNextToken(Doctrine_Query_Token::T_IDENTIFIER)) { $paramHolder->set('componentName', implode('.', $this->_identifiers)); // Will return an identifier, with the semantical check already applied $this->_identificationVariable = $this->AST('IdentificationVariable', $paramHolder); $paramHolder->remove('componentName'); } } public function semantical($paramHolder) { $parserResult = $this->_parser->getParserResult(); $componentName = implode('.', $this->_identifiers); if ($parserResult->hasQueryComponent($componentName)) { //echo "Entered in if of hasQueryComponent(".$componentName."): true\n"; // As long as name != alias, try to bring the queryComponent from name (already processed) $queryComponent = $parserResult->getQueryComponent($componentName); // Check if we defined _identificationVariable. We throw semantical error if not if ($this->_identificationVariable === null) { $componentName = $queryComponent['metadata']->getClassName(); $this->_parser->semanticalError( "Cannot re-declare component '{$componentName}'. Please assign an alias to it." ); return; } } else { //echo "Entered in if hasQueryComponent(".$componentName."), alias ".var_export($this->_identificationVariable, true).": false\n"; // No queryComponent was found. We will have to build it for the first time if (count($this->_identifiers) > 1) { // We are in a multiple identifier declaration; we are dealing with relations here $this->_semanticalWithMultipleIdentifier(); } else { // We are in a single identifier declaration; our identifier is the class name $this->_semanticalWithSingleIdentifier(); } } } public function buildSql() { // We need to bring the queryComponent and get things from there. $parserResult = $this->_parser->getParserResult(); // Retrieving connection $conn = $this->_em->getConnection(); return $conn->quoteIdentifier($this->_queryComponent['metadata']->getTableName()) . ' ' . $conn->quoteIdentifier($parserResult->getTableAliasFromComponentAlias($this->_identificationVariable)); } private function _semanticalWithSingleIdentifier() { $parserResult = $this->_parser->getParserResult(); // Get the connection for the component $conn = $this->_em->getConnection(); $componentName = $this->_identifiers[0]; // Retrieving ClassMetadata and Mapper try { $classMetadata = $this->_em->getClassMetadata($componentName); // Building queryComponent $this->_queryComponent = array( 'metadata' => $classMetadata, 'parent' => null, 'relation' => null, 'map' => null, 'scalar' => null, ); } catch (Doctrine_Exception $e) { //echo "Tried to load class metadata from '".$componentName."': " . $e->getMessage() . "\n"; $this->_parser->semanticalError($e->getMessage()); return; } if ($this->_identificationVariable === null) { $this->_identificationVariable = $componentName; } //echo "Identification Variable: " .$this->_identificationVariable . "\n"; $tableAlias = $parserResult->generateTableAlias($classMetadata->getClassName()); $parserResult->setQueryComponent($this->_identificationVariable, $this->_queryComponent); $parserResult->setTableAlias($tableAlias, $this->_identificationVariable); } private function _semanticalWithMultipleIdentifier() { $parserResult = $this->_parser->getParserResult(); // Get the connection for the component $conn = $this->_em->getConnection(); // Retrieve the base component try { $this->_queryComponent = $parserResult->getQueryComponent($this->_identifiers[0]); $classMetadata = $this->_queryComponent['metadata']; $className = $classMetadata->getClassName(); $parent = $path = $this->_identifiers[0]; } catch (Doctrine_Exception $e) { $this->_parser->semanticalError($e->getMessage()); return; } // We loop into others identifier to build query components for ($i = 1, $l = count($this->_identifiers); $i < $l; $i++) { $relationName = $this->_identifiers[$i]; $path .= '.' . $relationName; if ($parserResult->hasQueryComponent($path)) { // We already have the query component on hands, get it $this->_queryComponent = $parserResult->getQueryComponent($path); $classMetadata = $this->_queryComponent['metadata']; // If we are in our last check and identification variable is null, we throw semantical error if ($i == $l - 1 && $this->_identificationVariable === null) { $componentName = $classMetadata->getClassName(); $this->_parser->semanticalError( "Cannot re-declare component '{$componentName}' in path '{$path}'. " . "Please assign an alias to it." ); return; } } else { // We don't have the query component yet if ( ! $classMetadata->hasAssociation($relationName)) { $className = $classMetadata->getClassName(); $this->_parser->semanticalError("Relation '{$relationName}' does not exist in component '{$className}'"); return; } // Retrieving ClassMetadata try { $relation = $classMetadata->getAssociationMapping($relationName); $targetClassMetadata = $this->_em->getClassMetadata($relation->getTargetEntityName()); $this->_queryComponent = array( 'metadata' => $targetClassMetadata, 'parent' => $parent, 'relation' => $relation, 'map' => null, 'scalar' => null, ); $parent = $path; } catch (Doctrine_Exception $e) { //echo "Tried to load class metadata from '".$relationName."'\n"; $this->_parser->semanticalError($e->getMessage()); return; } } } if ($this->_identificationVariable === null) { $this->_identificationVariable = $path; } $tableAlias = $parserResult->generateTableAlias($targetClassMetadata->getClassName()); $parserResult->setQueryComponent($this->_identificationVariable, $this->_queryComponent); $parserResult->setTableAlias($tableAlias, $this->_identificationVariable); } /* Getters */ public function getIdentifiers() { return $this->_identifiers; } public function getQueryComponent() { return $this->_queryComponent; } public function getIdentificationVariable() { return $this->_identificationVariable; } }