Skip to content

Commit e59751d

Browse files
committed
Add jointure
1 parent 9062657 commit e59751d

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

src/QueryBuilder/QueryBuilder.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ class QueryBuilder implements QueryMethodsInterface
1111
public const ASC = 'ASC';
1212
public const DESC = 'DESC';
1313

14+
public const INNER = 'INNER';
15+
public const LEFT = 'LEFT';
16+
public const RIGHT = 'RIGHT';
17+
public const CROSS = 'CROSS';
18+
public const NATURAL = 'NATURAL';
19+
public const FULL = 'FULL';
20+
public const UNION = 'UNION';
21+
1422
public const SECURE = "_bubu_fw_end_secure_";
1523

1624
protected array $params = [];
@@ -37,10 +45,12 @@ public function __toString(): string
3745
{
3846
$request = str_replace('[TABLE_NAME]', $this->table, $this->action);
3947

40-
if (!is_null($this->condition)) $request .= $this->condition;
41-
if (!is_null($this->in)) $request .= (is_null($this->condition) ? 'WHERE ' : ' AND ') . implode(' AND ', $this->in);
42-
if (!is_null($this->orderBy)) $request .= $this->orderBy;
43-
if (!is_null($this->limit)) $request .= $this->limit;
48+
if (!is_null($this->as)) $request .= " AS {$this->as}";
49+
if (!is_null($this->join)) $request .= " {$this->join}";
50+
if (!is_null($this->condition)) $request .= $this->condition;
51+
if (!is_null($this->in)) $request .= (is_null($this->condition) ? 'WHERE ' : ' AND ') . implode(' AND ', $this->in);
52+
if (!is_null($this->orderBy)) $request .= $this->orderBy;
53+
if (!is_null($this->limit)) $request .= $this->limit;
4454

4555
return $request;
4656
}

src/QueryBuilder/QueryMethods.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ trait QueryMethods
1212
protected ?string $orderBy = null;
1313
protected ?string $limit = null;
1414
protected ?array $in = null;
15+
protected ?string $join = null;
1516

1617
public function table(string $table): self
1718
{
@@ -148,4 +149,24 @@ public function limit(int $limit, int $offset = 0): self
148149
$this->limit = " LIMIT $limit OFFSET $offset";
149150
return $this;
150151
}
152+
153+
/**
154+
* join
155+
*
156+
* @param string $table
157+
* @param string $method
158+
* @param string $col1
159+
* @param string $col2
160+
*
161+
* @return self
162+
*/
163+
public function join(
164+
string $table,
165+
string $method = QueryBuilder::INNER,
166+
string $col1,
167+
string $col2
168+
): self {
169+
$this->join .= " $method JOIN `$table` ON `$col1` = `$col2`";
170+
return $this;
171+
}
151172
}

src/QueryBuilder/QueryMethodsInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@ public function where(array ...$where): self;
1818
public function orderBy(string $column, string $order = QueryBuilder::ASC): self;
1919

2020
public function limit(int $limit, int $offset = 0): self;
21+
22+
public function join(string $table, string $mode, string $col1, string $col2): self;
2123
}

0 commit comments

Comments
 (0)