Jarves\ConditionOperator::subSelectConditionToSql PHP Method

subSelectConditionToSql() protected method

protected subSelectConditionToSql ( ConditionSubSelect $condition, array &$params, string $objectKey, array &$usedFieldNames = null ) : string
$condition Jarves\Configuration\ConditionSubSelect
$params array
$objectKey string
$usedFieldNames array
return string
    protected function subSelectConditionToSql(ConditionSubSelect $condition, &$params, $objectKey, array &$usedFieldNames = null)
    {
        $tableName = $condition->getTableNameSelect();
        if ($objectKey) {
            $def = $this->objects->getDefinition($objectKey);
            if ($def) {
                $tableName = $def->getTable();
            }
        }
        if ($condition->isTableNameSet()) {
            $tableName = $condition->getTableName();
        }
        $selected = [];
        foreach ($condition->getSelect() as $select) {
            if (false === strpos($select, '.')) {
                $select = $tableName . '.' . $select;
            }
            if (null !== $usedFieldNames) {
                $usedFieldNames[] = $select;
            }
            $selected[] = $select;
        }
        $selected = implode(', ', $selected);
        $joins = '';
        if ($condition->getJoins()) {
            $joins .= implode("\n", $condition->getJoins());
        }
        if ($condition->getSelfJoins()) {
            foreach ($condition->getSelfJoins() as $alias => $on) {
                $joins .= sprintf('JOIN %s as %s ON (%s)', $tableName, $alias, str_replace('%table%', $tableName, $on));
            }
        }
        $sql = sprintf('SELECT %s FROM %s %s', $selected, $tableName ?: $objectKey, $joins);
        if ($w = $this->standardConditionToSql($condition, $params, $objectKey, $usedFieldNames)) {
            $sql .= sprintf(' WHERE %s', $w);
        }
        if ($order = $condition->getOrder()) {
            $sql .= sprintf(' ORDER BY %s %s', $order[0], $order[1]);
        }
        return $sql;
    }