Skip to content
This repository has been archived by the owner on Oct 19, 2023. It is now read-only.

Commit

Permalink
Merge pull request #74 from EcomDev/fix-issue-with-table-prefix
Browse files Browse the repository at this point in the history
Fix issue with table alias on delete and truncate statements
  • Loading branch information
peterjaap committed Apr 30, 2021
2 parents 4eec814 + a6511af commit 39baae7
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/Elgentos/Masquerade/DataProcessor/TableService.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,26 @@ public function __construct(string $tableName, Connection $database, array $colu
/**
* Create a query with base select from this table
*
* @param bool $withoutAlias do not use alias for a main table
* @return Builder
*/
public function query(): Builder
public function query(bool $withoutAlias = false): Builder
{
return $this->database->query()->from(sprintf("%s as main", $this->tableName));
$tableExpression = $withoutAlias ? $this->tableName : sprintf("%s as main", $this->tableName);

return $this->database->query()->from($tableExpression);
}

/**
* Returns base select query with attached condition
*
* @param string $condition
* @param bool $withoutAlias do not use alias for a main table
* @return Builder
*/
public function queryWithCondition(string $condition): Builder
public function queryWithCondition(string $condition, bool $withoutAlias = false): Builder
{
$query = $this->query();
$query = $this->query($withoutAlias);

if ($condition) {
$query->whereRaw($condition);
Expand Down Expand Up @@ -105,7 +109,7 @@ public function queryWithJoinAndCondition(string $table, string $joinColumn, str
*/
public function delete(string $condition): void
{
$this->queryWithCondition($condition)->delete();
$this->queryWithCondition($condition, true)->delete();
}

/**
Expand Down Expand Up @@ -141,7 +145,7 @@ public function nullify(array $columns, ProgressNotifier $notifier, string $prim
*/
public function truncate(): void
{
$this->query()->truncate();
$this->query(true)->truncate();
}

/**
Expand Down

0 comments on commit 39baae7

Please sign in to comment.