public function testGetPrefixed() { $source = ['foo_a' => 'b_c', 'foo_b' => 'a_a', 'bar_c' => 'c_d', 'bar_d' => 'a_b']; $this->assertEquals(['a_a', 'a_b'], array_values(Utils::filterPrefixedElements($source, 'a_'))); $this->assertEquals(['a', 'b'], array_values(Utils::filterPrefixedElements($source, 'a_', Utils::FILTER_REMOVE_PREFIX))); $this->assertEquals(['foo_a' => 'b_c', 'foo_b' => 'a_a'], Utils::filterPrefixedElements($source, 'foo_', Utils::FILTER_USE_KEYS)); $this->assertEquals(['a' => 'b_c', 'b' => 'a_a'], Utils::filterPrefixedElements($source, 'foo_', Utils::FILTER_USE_KEYS | Utils::FILTER_REMOVE_PREFIX)); }
/** * @param Entity $entity * @param Select $query * @param array $with * @param string $prefix * * @return Select */ private function joinRelationsToQuery(Entity $entity, Select $query, array $with, $prefix = '') { if ($prefix === '') { $leftAlias = $this->alias ?: $entity->getTable(); } else { $leftAlias = $prefix; $prefix .= '_'; } foreach (array_filter($with, [$entity, 'hasRelation']) as $relationName) { $relation = $entity->getRelation($relationName); $relatedEntity = $relation->getEntity(); $alias = $prefix . $relationName; $query->addSelect(array_map(function ($item) use($alias) { return "{$alias}.{$item} as {$alias}_{$item}"; }, array_values($relatedEntity->getFieldNames()))); $relation->joinToQuery($query, $leftAlias, $alias); $strippedWith = Utils::filterPrefixedElements($with, $relationName . '.', Utils::FILTER_REMOVE_PREFIX); $this->joinRelationsToQuery($relatedEntity, $query, $strippedWith, $alias); } return $query; }