private function joinRelatedTable($currentTable, $referencingColumn, $targetTable, $targetTablePrimaryKey, $filters = array(), $joinImmediately = TRUE) { // Join if not already joined. if (!$this->getTableAlias($currentTable, $targetTable, $referencingColumn, $globalKey, $alias)) { if (empty($filters)) { // Do simple join. // In few cases there is no need to do join immediately -> register join // to decide later. $this->registerJoin($currentTable, $referencingColumn, $targetTable, $targetTablePrimaryKey, $globalKey, $alias); $joinImmediately && $this->triggerJoin(); } else { // Join sub-query due to applying implicit filters. $subFluent = new Fluent($this->fluent->getConnection()); $subFluent->select('%n.*', $targetTable)->from($targetTable); // Apply implicit filters. $targetedArgs = array(); if ($filters instanceof ImplicitFilters) { $targetedArgs = $filters->getTargetedArgs(); $filters = $filters->getFilters(); } foreach ($filters as $filter) { $args = array($filter); if (is_string($filter) && array_key_exists($filter, $targetedArgs)) { $args = array_merge($args, $targetedArgs[$filter]); } call_user_func_array(array($subFluent, 'applyFilter'), $args); } $this->fluent->leftJoin($subFluent, "[{$alias}]")->on("[{$currentTable}].[{$referencingColumn}] = [{$alias}].[{$targetTablePrimaryKey}]"); $this->registerTableAlias($globalKey, $alias); } } return $alias; }