/** * Select the given field expressions. * * @param $fieldExpression String The field to select (escaped SQL statement) * @param $alias String The alias of that field (escaped SQL statement) */ public function selectField($fieldExpression, $alias = null) { $this->query->selectField($fieldExpression, $alias); }
/** * Invoked prior to getFinalisedQuery() * * @param DataQuery $dataQuery * @param array $queriedColumns * @param SQLSelect $sqlSelect */ public function beforeGetFinalisedQuery(DataQuery $dataQuery, $queriedColumns = [], SQLSelect $sqlSelect) { // Get metadata and SQL from join table $hasManyRelation = $this->getParentRelationship($dataQuery); $joinTableSQLSelect = $hasManyRelation->dataQuery()->query(); $joinTableSQL = $joinTableSQLSelect->sql($joinTableParameters); $joinTableColumns = array_keys($joinTableSQLSelect->getSelect()); // Get aliases (keys) only $joinTableAlias = $this->getJoinAlias(); // Get fields to join on $localKey = $this->getLocalKey(); $schema = DataObject::getSchema(); $baseTable = $schema->baseDataClass($dataQuery->dataClass()); $childField = $schema->sqlColumnForField($baseTable, 'ID'); // Add select fields foreach ($joinTableColumns as $joinTableColumn) { $sqlSelect->selectField("\"{$joinTableAlias}\".\"{$joinTableColumn}\"", "{$joinTableAlias}_{$joinTableColumn}"); } // Apply join and record sql for later insertion (at end of replacements) $sqlSelect->addInnerJoin('(SELECT $$_SUBQUERY_$$)', "\"{$joinTableAlias}\".\"{$localKey}\" = {$childField}", $joinTableAlias, 20, $joinTableParameters); $dataQuery->setQueryParam('Foreign.JoinTableSQL', $joinTableSQL); // After this join, and prior to afterGetFinalisedQuery, $sqlSelect will be populated with the // necessary sql rewrites (versioned, etc) that effect the base table. // By using a placeholder for the subquery we can protect the subquery (already rewritten) // from being re-written a second time. However we DO want the join predicate (above) to be rewritten. // See http://php.net/manual/en/function.str-replace.php#refsect1-function.str-replace-notes // for the reason we only add the final substitution at the end of getFinalisedQuery() }