From a6511af1c8cd3c34e5412558f2507a14fe0fc2ac Mon Sep 17 00:00:00 2001 From: Ivan Chepurnyi Date: Thu, 29 Apr 2021 21:21:35 +0200 Subject: [PATCH] Fix issue with table alias on delete and truncate statements --- .../Masquerade/DataProcessor/TableService.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Elgentos/Masquerade/DataProcessor/TableService.php b/src/Elgentos/Masquerade/DataProcessor/TableService.php index a19d09d..ca05f26 100644 --- a/src/Elgentos/Masquerade/DataProcessor/TableService.php +++ b/src/Elgentos/Masquerade/DataProcessor/TableService.php @@ -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); @@ -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(); } /** @@ -141,7 +145,7 @@ public function nullify(array $columns, ProgressNotifier $notifier, string $prim */ public function truncate(): void { - $this->query()->truncate(); + $this->query(true)->truncate(); } /**