Skip to content

Commit 990e748

Browse files
committed
ManyToMany: Escape with MySQL syntax
1 parent 6d2eefc commit 990e748

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,10 @@ TDBM (The DataBase Machine)
1212

1313
Check out [the documentation at https://thecodingmachine.github.io/tdbm/](https://thecodingmachine.github.io/tdbm/).
1414

15+
## Run the test locally
16+
17+
### Postgres
18+
19+
Run an instance with `docker run -p 5432:5432 -e POSTGRES_HOST_AUTH_METHOD=trust postgres:12`
20+
21+
Run the tests with `vendor/bin/phpunit -c phpunit.postgres.xml`

src/TDBMService.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -1474,10 +1474,13 @@ private function fromCache(string $key, callable $closure)
14741474
*/
14751475
public function _getRelatedBeans(ManyToManyRelationshipPathDescriptor $pathDescriptor, AbstractTDBMObject $bean): ResultIterator
14761476
{
1477+
// Magic Query expect MySQL syntax for quotes
1478+
$platform = new MySqlPlatform();
1479+
14771480
return $this->findObjectsFromSql(
14781481
$pathDescriptor->getTargetName(),
1479-
$pathDescriptor->getPivotFrom($this->connection),
1480-
$pathDescriptor->getPivotWhere($this->connection),
1482+
$pathDescriptor->getPivotFrom($platform),
1483+
$pathDescriptor->getPivotWhere($platform),
14811484
$pathDescriptor->getPivotParams($this->getPrimaryKeyValues($bean)),
14821485
null,
14831486
null,

src/Utils/ManyToManyRelationshipPathDescriptor.php

+21-10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace TheCodingMachine\TDBM\Utils;
44

55
use Doctrine\DBAL\Connection;
6+
use Doctrine\DBAL\Platforms\AbstractPlatform;
67
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
78
use TheCodingMachine\TDBM\ResultIterator;
89
use TheCodingMachine\TDBM\TDBMInvalidArgumentException;
@@ -13,11 +14,11 @@
1314
class ManyToManyRelationshipPathDescriptor
1415
{
1516
/**
16-
* @var string
17+
* @var string Unquoted identifier of the target table
1718
*/
1819
private $targetTable;
1920
/**
20-
* @var string
21+
* @var string Unquoted identifier of the pivot table
2122
*/
2223
private $pivotTable;
2324
/**
@@ -69,7 +70,12 @@ public function getTargetName(): string
6970
return $this->targetTable;
7071
}
7172

72-
public function getPivotFrom(Connection $connection): string
73+
/**
74+
* Get the `FROM` clause of the query.
75+
*
76+
* This may have issue handling namespaced table (e.g. mydb.table)
77+
*/
78+
public function getPivotFrom(AbstractPlatform $platform): string
7379
{
7480
$mainTable = $this->targetTable;
7581
$pivotTable = $this->pivotTable;
@@ -78,21 +84,26 @@ public function getPivotFrom(Connection $connection): string
7884
foreach ($this->joinForeignKeys as $key => $column) {
7985
$join[] = sprintf(
8086
'%s.%s = %s.%s',
81-
$connection->quoteIdentifier($mainTable),
82-
$connection->quoteIdentifier($column),
83-
$connection->quoteIdentifier($pivotTable),
84-
$connection->quoteIdentifier($this->joinLocalKeys[$key])
87+
$platform->quoteIdentifier($mainTable),
88+
$platform->quoteIdentifier($column),
89+
$platform->quoteIdentifier($pivotTable),
90+
$platform->quoteIdentifier($this->joinLocalKeys[$key])
8591
);
8692
}
8793

88-
return $connection->quoteIdentifier($mainTable) . ' JOIN ' . $connection->quoteIdentifier($pivotTable) . ' ON ' . implode(' AND ', $join);
94+
return $platform->quoteIdentifier($mainTable) . ' JOIN ' . $platform->quoteIdentifier($pivotTable) . ' ON ' . implode(' AND ', $join);
8995
}
9096

91-
public function getPivotWhere(Connection $connection): string
97+
/**
98+
* Get the `WHERE` clause of the query.
99+
*
100+
* This may have issue handling namespaced table (e.g. mydb.table)
101+
*/
102+
public function getPivotWhere(AbstractPlatform $platform): string
92103
{
93104
$paramList = [];
94105
foreach ($this->whereKeys as $key => $column) {
95-
$paramList[] = sprintf('%s.%s = :param%s', $connection->quoteIdentifier($this->pivotTable), $connection->quoteIdentifier($column), $key);
106+
$paramList[] = sprintf('%s.%s = :param%s', $platform->quoteIdentifier($this->pivotTable), $platform->quoteIdentifier($column), $key);
96107
}
97108
return implode(' AND ', $paramList);
98109
}

0 commit comments

Comments
 (0)