/** * Propel uses for his nested-set objects `lft` and `rgt` fields. * So we include with this condition all entries 'inside' the entry * defined through $condition. * * @param Condition $condition * @return Condition */ public function getNestedSubCondition(Condition $condition) { $result = new Condition(null, $this->jarves); $sub = new ConditionSubSelect(null, $this->jarves); $sub->select('sub.lft'); $sub->addSelfJoin('sub', 'sub.lft BETWEEN %table%.lft+1 AND %table%.rgt-1'); $sub->setRules($condition->getRules()); $subCondition = new Condition(); $subCondition->addAnd(['lft', 'IN', $sub]); $result->addAnd($subCondition); return $result; }
/** * @param ConditionSubSelect $condition * @param array $params * @param string $objectKey * @param array|null $usedFieldNames * @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; }