From bfd844bc9d5580cefede20c9fc9a3ea5eed9c979 Mon Sep 17 00:00:00 2001 From: Hannes Papenberg Date: Thu, 12 Nov 2026 23:40:56 +0100 Subject: [PATCH] Add DatabaseQuery::bindValue() for binding literals and copies bind() takes the value by reference, so literals cannot be bound (bind(':id', 5) is a fatal error), and binding a loop variable binds every placeholder to its final value. bindValue() binds a copy of the value. The by-reference behaviour of bind() is now documented in its docblock. Co-Authored-By: Claude Opus 5.5 --- Tests/DatabaseQueryTest.php | 17 +++++++++++++++++ src/DatabaseQuery.php | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/Tests/DatabaseQueryTest.php b/Tests/DatabaseQueryTest.php index 96333637..132e6df3 100644 --- a/Tests/DatabaseQueryTest.php +++ b/Tests/DatabaseQueryTest.php @@ -51,6 +51,23 @@ protected function setUp(): void $this->query = new TestDatabaseQuery($this->db); } + #[TestDox('The bindValue method binds a copy of the value')] + public function testBindValue() + { + $this->assertSame($this->query, $this->query->bindValue(':literal', 'JustValueNoReference'), 'The query builder supports method chaining'); + + foreach ([1, 2, 3] as $i) { + $this->query->bindValue(':id' . $i, $i, ParameterType::INTEGER); + } + + $bounded = $this->query->getBounded(); + + $this->assertSame('JustValueNoReference', $bounded[':literal']->value); + $this->assertSame(ParameterType::STRING, $bounded[':literal']->dataType); + $this->assertSame([1, 2, 3], [$bounded[':id1']->value, $bounded[':id2']->value, $bounded[':id3']->value], 'Each binding keeps its own value'); + $this->assertSame(ParameterType::INTEGER, $bounded[':id2']->dataType); + } + #[TestDox('The call method correctly creates and manages a CALL query element')] public function testCall() { diff --git a/src/DatabaseQuery.php b/src/DatabaseQuery.php index 6abc16f8..cbe381c4 100644 --- a/src/DatabaseQuery.php +++ b/src/DatabaseQuery.php @@ -1866,9 +1866,43 @@ public function andWhere($conditions, $glue = 'OR') return $this->extendWhere('AND', $conditions, $glue); } + /** + * Method to add a value to an internal array that will be bound to a prepared SQL statement before query execution. + * + * Unlike bind(), the value is copied, so literals can be bound and changing a variable afterwards does not change + * the bound value. + * + * @param array|string|integer $key The key that will be used in your SQL query to reference the value. Usually of + * the form ':key', but can also be an integer. + * @param mixed $value The value that will be bound. It can be an array, in this case it has to be + * same length of $key. + * @param array|string $dataType Constant corresponding to a SQL datatype. It can be an array, in this case it + * has to be same length of $key + * @param integer $length The length of the variable. Usually required for OUTPUT parameters. + * @param array $driverOptions Optional driver options to be used. + * + * @return $this + * + * @since __DEPLOY_VERSION__ + * @throws \InvalidArgumentException + */ + public function bindValue( + array|string|int $key, + mixed $value, + array|string $dataType = ParameterType::STRING, + int $length = 0, + array $driverOptions = [] + ): static { + // $value is a copy here, so bind() references the copy instead of the caller's variable + return $this->bind($key, $value, $dataType, $length, $driverOptions); + } + /** * Method to add a variable to an internal array that will be bound to a prepared SQL statement before query execution. * + * The variable is bound by reference: the value it has when the query is executed is used, not the value it has + * when bind() is called. Use bindValue() to bind a literal or the current value of a variable. + * * @param array|string|integer $key The key that will be used in your SQL query to reference the value. Usually of * the form ':key', but can also be an integer. * @param mixed $value The value that will be bound. It can be an array, in this case it has to be