Allow to specify a custom alias for joined tables.

This commit is contained in:
Madeorsk 2024-11-09 10:30:16 +01:00
parent 22c63043e6
commit 3b2658e423
Signed by: Madeorsk
GPG key ID: 677E51CA765BB79F
3 changed files with 19 additions and 12 deletions

View file

@ -277,7 +277,8 @@ class PostgreSqlAdapter extends DatabaseAdapter
foreach ($joins as $join) foreach ($joins as $join)
{ // For each join clause, build its conditions and append the built bindings. { // For each join clause, build its conditions and append the built bindings.
[$onSql, $onBindings] = $this->buildWhere($join->getConditions(), "ON"); [$onSql, $onBindings] = $this->buildWhere($join->getConditions(), "ON");
$sql .= "$join->type JOIN \"$join->table\" $onSql "; $asSql = !empty($join->as) ? " AS $join->as" : "";
$sql .= "$join->type JOIN \"$join->table\"$asSql $onSql ";
array_push($bindings, ...$onBindings); array_push($bindings, ...$onBindings);
} }

View file

@ -27,11 +27,12 @@ trait HasJoin
* Create a new join clause. * Create a new join clause.
* @param string $type Type of the join clause. * @param string $type Type of the join clause.
* @param string $table Joined table. * @param string $table Joined table.
* @param string|null $as Joined table alias.
* @return JoinBuilder New join clause builder. * @return JoinBuilder New join clause builder.
*/ */
public function join(string $type, string $table): JoinBuilder public function join(string $type, string $table, ?string $as = null): JoinBuilder
{ {
$join = new JoinBuilder($this, $type, $table); $join = new JoinBuilder($this, $type, $table, $as);
$this->joins[] = $join; $this->joins[] = $join;
return $join; return $join;
} }
@ -39,40 +40,44 @@ trait HasJoin
/** /**
* Create a new inner join clause. * Create a new inner join clause.
* @param string $table Joined table. * @param string $table Joined table.
* @param string|null $as Joined table alias.
* @return JoinBuilder New join clause builder. * @return JoinBuilder New join clause builder.
*/ */
public function innerJoin(string $table): JoinBuilder public function innerJoin(string $table, ?string $as = null): JoinBuilder
{ {
return $this->join(JoinBuilder::INNER, $table); return $this->join(JoinBuilder::INNER, $table, $as);
} }
/** /**
* Create a new outer join clause. * Create a new outer join clause.
* @param string $table Joined table. * @param string $table Joined table.
* @param string|null $as Joined table alias.
* @return JoinBuilder New join clause builder. * @return JoinBuilder New join clause builder.
*/ */
public function outerJoin(string $table): JoinBuilder public function outerJoin(string $table, ?string $as = null): JoinBuilder
{ {
return $this->join(JoinBuilder::OUTER, $table); return $this->join(JoinBuilder::OUTER, $table, $as);
} }
/** /**
* Create a new left join clause. * Create a new left join clause.
* @param string $table Joined table. * @param string $table Joined table.
* @param string|null $as Joined table alias.
* @return JoinBuilder New join clause builder. * @return JoinBuilder New join clause builder.
*/ */
public function leftJoin(string $table): JoinBuilder public function leftJoin(string $table, ?string $as = null): JoinBuilder
{ {
return $this->join(JoinBuilder::LEFT, $table); return $this->join(JoinBuilder::LEFT, $table, $as);
} }
/** /**
* Create a new right join clause. * Create a new right join clause.
* @param string $table Joined table. * @param string $table Joined table.
* @param string|null $as Joined table alias.
* @return JoinBuilder New join clause builder. * @return JoinBuilder New join clause builder.
*/ */
public function rightJoin(string $table): JoinBuilder public function rightJoin(string $table, ?string $as = null): JoinBuilder
{ {
return $this->join(JoinBuilder::RIGHT, $table); return $this->join(JoinBuilder::RIGHT, $table, $as);
} }
} }

View file

@ -15,8 +15,9 @@ class JoinBuilder
* @param mixed $query The calling query. * @param mixed $query The calling query.
* @param string $type Join type (INNER, OUTER, LEFT, RIGHT, ...). * @param string $type Join type (INNER, OUTER, LEFT, RIGHT, ...).
* @param string $table The table to join. * @param string $table The table to join.
* @param string|null $as Joined table alias.
*/ */
public function __construct(protected mixed $query, public readonly string $type, public readonly string $table) public function __construct(protected mixed $query, public readonly string $type, public readonly string $table, public readonly ?string $as = null)
{} {}
/** /**