Skip to content

[4.x] Add DatabaseQuery::bindValue() for binding literals and copies - #405

Open
Hackwar wants to merge 1 commit into
joomla-framework:4.x-devfrom
Hackwar:4.x-query-bindvalue
Open

Hackwar wants to merge 1 commit into
joomla-framework:4.x-devfrom
Hackwar:4.x-query-bindvalue

Conversation

@Hackwar

@Hackwar Hackwar commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Summary of Changes

DatabaseQuery::bind() takes $value by reference. That is intentional (output parameters, rebinding a variable between executions), but it means:

  • a literal cannot be bound: $query->bind(':id', 5) is a fatal error;
  • binding a loop variable binds every placeholder to the value the variable has at execution time, i.e. the last one.

Users work around it with temporary variables or misuse bindArray(). The new bindValue($key, $value, $dataType = ParameterType::STRING, $length = 0, $driverOptions = []) binds a copy of the value: it is a by-value proxy to bind(), so it supports the same arguments (including arrays of keys and values). New API gets native parameter types and returns static. The by-reference semantics of bind() are now spelled out in its docblock.

bindValue() is added to DatabaseQuery only. Adding it to QueryInterface would break other implementations of the interface, so that is a 5.0 change.

Alternative to #357 (HLeithner), which adds the same proxy but still needed attribute-based tests.

Testing Instructions

New DatabaseQueryTest::testBindValue binds a literal and three values from a loop and checks that each binding keeps its own value (vendor/bin/phpunit Tests/DatabaseQueryTest.php).

Documentation Changes Required

None on 4.x-dev (no page there documents binding yet). #373 states "There is no bindValue()" in docs/overview.md → "Things to know before you build on this" and describes the workarounds in docs/parameters-and-escaping.md → "Binding by reference"; when the second of the two is merged, both should point to bindValue() instead.

🤖 Generated with Claude Code

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 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant