Replace \PDO::FETCH_* constants to indicate fetch mode with an enumeration
Closes #3487851
Merge request reports
Activity
added 4 commits
-
e9399b02...91fc116a - 2 commits from branch
project:11.x
- 72281b9a - Merge branch '11.x' into 3487851-replace-pdofetch-constants
- 9a218caa - introduce PdoTrait and a new Statement namespace
-
e9399b02...91fc116a - 2 commits from branch
added 1 commit
- 416aa232 - PDO::FETCH_PROPS_LATE should be used for ClassObject mapping
103 * @param \Drupal\Core\Database\FetchAs|null $mode 104 * (Optional) one of the cases of the FetchAs enum. If not specified, 105 * defaults to what is specified by setFetchMode(). 106 * @param int|null $cursorOrientation 107 * Not implemented in all database drivers, don't use. 108 * @param int|null $cursorOffset 109 * Not implemented in all database drivers, don't use. 110 * 111 * @return array<string|int|float|bool>|object|false 112 * A result, formatted according to $mode, or FALSE on failure. 113 */ 114 protected function clientFetch(?FetchAs $mode = NULL, ?int $cursorOrientation = NULL, ?int $cursorOffset = NULL) { 115 return $this->getClientStatement()->fetch( 116 $mode ? $this->fetchAsToPdo($mode) : \PDO::FETCH_DEFAULT, 117 $cursorOrientation ?? \PDO::FETCH_ORI_NEXT, 118 $cursorOffset ?? 0, - Comment on lines +117 to +118
We are changing how we call the PDO fetch() method. We used to only set the second and third parameter when they are not null. Now they are set with a default value. Unless there is a good reason to set with a default value, can we change it back to how we are doing it now. I do not like that this change will result in a BC break.
for reference: https://3v4l.org/Q6JqP shows how
func_num_args()
returns the number of args actually passed on the call.Edited by mondrakechanged this line in version 15 of the diff
- Resolved by mondrake
1 <?php 2 3 declare(strict_types=1); 4 5 namespace Drupal\Core\Database\Statement; 6 7 /** 8 * A trait for calling \PDOStatement methods. 9 */ 10 trait PdoTrait { 11 12 protected function fetchAsToPdo(FetchAs $mode): int { 13 return match ($mode) { 14 FetchAs::Associative => \PDO::FETCH_ASSOC, 15 FetchAs::ClassObject => \PDO::FETCH_CLASS | \PDO::FETCH_PROPS_LATE, Support for
\PDO::FETCH_CLASS | \PDO::FETCH_CLASSTYPE
was already deprecated in 10.2 and dropped in 11: https://www.drupal.org/node/3377999\PDO::FETCH_CLASS
without\PDO::FETCH_PROPS_LATE
cannot be supported. Props late means that the values are associated to the object properties after the object constructor is called. That is the only possible way if we want to allow non-PDO drivers to emulate the mode. Apparently PDO can work on low level to get the properties assigned before calling the constructor, but I am not aware of such a possibility in pure PHP.Just a note that SQLite in HEAD is already missing the possibility to define properties before construction, because StatementPrefetchIterator does this: https://git.drupalcode.org/project/drupal/-/blob/11.x/core/lib/Drupal/Core/Database/StatementPrefetchIterator.php?ref_type=heads#L248,
and
assocToClass
does instantiate the class before setting the properties:protected function assocToClass(array $rowAssoc, string $className, array $constructorArguments): object { $classObj = new $className(...$constructorArguments); foreach ($rowAssoc as $column => $value) { $classObj->$column = $value; } return $classObj; }
Edited by mondrake
- Resolved by mondrake
added 21 commits
-
793b709e...1c33460a - 19 commits from branch
project:11.x
- 147aeb79 - Merge branch '11.x' into 3487851-replace-pdofetch-constants
- b9aa7a16 - fix fetch
-
793b709e...1c33460a - 19 commits from branch