public function shouldJoin($key, $prefix = null)
    {
        $parts = explode('.', $key);

        if (!$prefix) {
            $prefix = $this->getRootAlias();
        }

        if (!in_array($parts[0], $this->qb->getAllAliases())) {
            $this->qb->leftJoin($prefix.'.'.$parts[0], $parts[0]);
        }

        // If the key consists of multiple . parts, we also need to add joins for the other parts
        if (count($parts) > 2) {
            $prefix = array_shift($parts);
            $leftover = implode('.', $parts);
            $key = $this->shouldJoin($leftover, $prefix);
        }

        return $key;
    }