Skip to content

Commit ebdf2f0

Browse files
committed
Allow set multiple JOINs
1 parent 13e4e3e commit ebdf2f0

2 files changed

Lines changed: 37 additions & 17 deletions

File tree

src/Manipulation/Traits/Join.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ private function setJoin(
365365
string $clause = null,
366366
Closure | array $expression = null
367367
) : static {
368-
$this->sql['join'] = [
368+
$this->sql['join'][] = [
369369
'type' => $type,
370370
'table' => $table,
371371
'clause' => $clause,
@@ -384,17 +384,24 @@ protected function renderJoin() : ?string
384384
if ( ! isset($this->sql['join'])) {
385385
return null;
386386
}
387-
$type = $this->renderJoinType($this->sql['join']['type']);
388-
$conditional = $this->renderJoinConditional(
389-
$type,
390-
$this->sql['join']['table'],
391-
$this->sql['join']['clause'],
392-
$this->sql['join']['expression']
393-
);
394-
if ($type) {
395-
$type .= ' ';
387+
$result = '';
388+
foreach ($this->sql['join'] as $index => $join) {
389+
$type = $this->renderJoinType($join['type']);
390+
$conditional = $this->renderJoinConditional(
391+
$type,
392+
$join['table'],
393+
$join['clause'],
394+
$join['expression']
395+
);
396+
if ($type) {
397+
$type .= ' ';
398+
}
399+
if ($index > 0) {
400+
$result .= \PHP_EOL;
401+
}
402+
$result .= " {$type}JOIN {$conditional}";
396403
}
397-
return " {$type}JOIN {$conditional}";
404+
return $result;
398405
}
399406

400407
/**

tests/Manipulation/Traits/JoinTest.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,30 @@ public function testJoin() : void
8181
{
8282
self::assertNull($this->statement->renderJoin());
8383
$this->statement->join('users');
84-
self::assertSame(' JOIN `users`', $this->statement->renderJoin());
84+
self::assertSame(
85+
' JOIN `users`',
86+
$this->statement->renderJoin()
87+
);
88+
}
89+
90+
public function testMultipleJoins() : void
91+
{
92+
self::assertNull($this->statement->renderJoin());
93+
$this->statement->join('users');
94+
self::assertSame(
95+
' JOIN `users`',
96+
$this->statement->renderJoin()
97+
);
8598
$this->statement->join('users', 'natural');
86-
self::assertSame(' NATURAL JOIN `users`', $this->statement->renderJoin());
87-
$this->statement->join('users', 'cross', 'using', ['user_id']);
8899
self::assertSame(
89-
' CROSS JOIN `users` USING (`user_id`)',
100+
' JOIN `users`' . \PHP_EOL . ' NATURAL JOIN `users`',
90101
$this->statement->renderJoin()
91102
);
92-
$this->statement->join('users', 'left', 'on', static fn () => 'profiles.user_id = users.id');
103+
$this->statement->join('foo', 'inner');
93104
self::assertSame(
94-
' LEFT JOIN `users` ON (profiles.user_id = users.id)',
105+
' JOIN `users`' . \PHP_EOL .
106+
' NATURAL JOIN `users`' . \PHP_EOL .
107+
' INNER JOIN `foo`',
95108
$this->statement->renderJoin()
96109
);
97110
}

0 commit comments

Comments
 (0)