-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Predictable QueryBuilder::executeQuery()
and QueryBuilder::executeStatement()
#4578
Predictable QueryBuilder::executeQuery()
and QueryBuilder::executeStatement()
#4578
Conversation
1034d56
to
d9b67ba
Compare
If we do this change, then we could look to add this |
The naming problem is not on our end. The "statement" on the left-hand side is a prepared statement. The "statement" on the right-hand side is a SQL statement (DDL/DML/DCL) as opposed to an SQL query. Having it named as |
Looks good to me. @beberlei do you want to backport it to |
@morozov the query builder change is ok for 3.1.x I believe, since the API for QueryBuilder::execute() does not return a boolean, it is already ok to use, would be a new migration step in the future for users. I don't believe there is pressure moving this to 2.13 |
#4580 would be the one that is more interesting for the 2.x branches |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@PowerKiKi please add a runtime deprecation using Deprecation::trigger()
.
I added the |
8975f66
to
c8f1a38
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good overall. Please squash at the end.
@@ -300,19 +300,57 @@ public function fetchFirstColumn(): array | |||
return $this->connection->fetchFirstColumn($this->getSQL(), $this->params, $this->paramTypes); | |||
} | |||
|
|||
/** | |||
* Executes an SQL query (SELECT) and returns a Result. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This may be misleading since it works also for DESCRIBE
, SHOW
and so on. Let's rephrase to:
Executes an SQL query that returns a Result.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd agree with that on the Connection
class, but AFAIK, there is no way to build anything else than a SELECT
query with a QueryBuilder
. So I don't think we should document an impossible use-case.
WDYT ?
see:
dbal/src/Query/QueryBuilder.php
Lines 40 to 46 in 461b3b7
/* | |
* The query types. | |
*/ | |
public const SELECT = 0; | |
public const DELETE = 1; | |
public const UPDATE = 2; | |
public const INSERT = 3; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, I didn't take the scope into account. Sorry for the confusion.
/** | ||
* Executes an SQL statement and returns the number of affected rows. | ||
* | ||
* Should be used for INSERT, UPDATE and DELETE |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not limited to DML (INSERT
, etc.), it could be also DDL and other non-DQL statements. See JDBC for reference.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Idem, the QueryBuilder
cannot build anything else than DML. Should we really mention that ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, you're right.
…Statement()` This deprecates `QueryBuilder::execute()`, because its return type is unpredictable and raises issues with static analysis tools such as PHPStan. Instead you should use either `QueryBuilder::executeQuery()` or `QueryBuilder::executeStatement()`, depending on whether the queryBuilder is a query (SELECT) or a statement (INSERT, UPDATE, DELETE). You might also consider the use of the new shortcut methods, such as: - `fetchAllAssociative()` - `fetchAllAssociativeIndexed()` - `fetchAllKeyValue()` - `fetchAllNumeric()` - `fetchAssociative()` - `fetchFirstColumn()` - `fetchNumeric()` - `fetchOne()` This commit is a direct follow-up to doctrine#4461 where those shortcut methods where introduced.
100cde2
to
7b5dd78
Compare
Thanks, @PowerKiKi. |
Summary
This deprecates
QueryBuilder::execute()
, because its return type isunpredictable and raises issues with static analysis tools such as PHPStan.
Instead you should use either
QueryBuilder::executeQuery()
orQueryBuilder::executeStatement()
, depending on whether the queryBuilder is a query (SELECT)or a statement (INSERT, UPDATE, DELETE).
You might also consider the use of the new shortcut methods, such as:
fetchAllAssociative()
fetchAllAssociativeIndexed()
fetchAllKeyValue()
fetchAllNumeric()
fetchAssociative()
fetchFirstColumn()
fetchNumeric()
fetchOne()
This commit is a direct follow-up to #4461
where those shortcut methods where introduced.