Skip to content
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

[BUG]: PdoResult::fetchAll() does not accept string for class name in FETCH_CLASS mode #16177

Closed
ccc-emile-antognetti opened this issue Oct 24, 2022 · 2 comments · Fixed by #16178
Assignees
Labels
5.0 The issues we want to solve in the 5.0 release bug A bug report status: high High

Comments

@ccc-emile-antognetti
Copy link

Questions? Forum: https://phalcon.io/forum or Discord: https://phalcon.io/discord

Describe the bug
A clear and concise description of what the bug is.
When calling fetchAll on a Result\PdoResult object using the \PDO::FETCH_CLASS mode, the following error is returned :

Argument 2 passed to Phalcon\Db\Result\PdoResult::fetchAll() must be of the type int, string given

To Reproduce

Provide output if related. Provide coredump if any. Use https://docs.phalcon.io/en/latest/generating-backtrace as reference.

#0 /var/www/html/phalcon5.[REDACTED].com/app/Model/Repository/CompanyRepo.php(29): Phalcon\Db\Result\PdoResult->fetchAll()

#1 /var/www/html/phalcon5.[REDACTED].com/app/Model/WebUser.php(368): [REDACTED]\Model\Repository\CompanyRepo::[REDACTED]()

#2 /var/www/html/phalcon5.[REDACTED].com/app/Auth/LoginTrait.php(21): [REDACTED]\Model\WebUser->shouldSeePasswordUpdateNotice()

#3 /var/www/html/phalcon5.[REDACTED].com/app/Controller/DefaultController.php(330): CareerCross\Controller\DefaultController->buildLoginRedirectUsingString()

#4 [internal function]: [REDACTED]\Controller\DefaultController->loginAction()

#5 [internal function]: Phalcon\Dispatcher\AbstractDispatcher->callActionMethod()

#6 [internal function]: Phalcon\Dispatcher\AbstractDispatcher->dispatch()

#7 /var/www/html/phalcon5.[REDACTED].com/public/index.php(15): Phalcon\Mvc\Application->handle()


#8 {main}

Steps to reproduce the behavior:

Call fetchAll on a Result\PdoResult object using FETCH_CLASS mode

Provide minimal script to reproduce the issue

use Phalcon\Db\Adapter\Pdo\Mysql as Connection;

$connection = new Connection(
                [
                    'host' => foo,
                    'username' => bar,
                    'password' => foo,
                    'dbname' => bar,
                    'port' => foo,
                    'charset' => 'utf8',
                    'dialectClass' => new \Phalcon\Db\Dialect\MySQL();
                    'persistent' => false,
                ]
            );

$sql = "SELECT * FROM automobiles WHERE date_manufactured = :date";

$result = $connection->query($sql, ['date' => '1998'])->fetchAll(\PDO::FETCH_CLASS, Model\Automobiles::class, null);
var_dump($result);
// paste output here
Argument 2 passed to Phalcon\Db\Result\PdoResult::fetchAll() must be of the type int, string given

Expected behavior
fetchAll should return instances of the specified class, mapping the columns of each row to named properties in the class.
https://www.php.net/manual/en/pdostatement.fetchall.php

// paste code
[
   0 => 'Model\Automobiles' {
               [...]
              "date_manufactured" : "1998"
              }
    1 => (so forth so on)
]

Screenshots
If applicable, add screenshots to help explain your problem.

Details

  • Phalcon version: 5.0.4
  • PHP Version: PHP 7.4.32
  • Operating System: Linux 5.15.0-50-generic can i use Phalcon 0.5 for product. #56~20.04.1-Ubuntu
  • Installation type: Compiling from source
  • Zephir version (if any): Version 0.16.3-$Id$
  • Server: Apache
  • Other related info (Database, table schema): N/A

Additional context
The error seems to be thrown because in Phalcon 5, the second argument of the fetchAll function is specified as an int

public function fetchAll(int fetchStyle = null, int fetchArgument = Pdo::FETCH_ORI_NEXT, int ctorArgs = 0) -> array

Previously in Phalcon 4, no type was set for this argument.
public function fetchAll(var fetchStyle = null, var fetchArgument = null, var ctorArgs = null) -> array
{

Restricting this argument to an int is problematic because in the FETCH_CLASS mode, the second argument should be a string used to specify the Class that the results should be mapped to

public PDOStatement::fetchAll(int $mode = PDO::FETCH_CLASS, string $class, ?array $constructorArgs): array

https://www.php.net/manual/en/pdostatement.fetchall.php

@ccc-emile-antognetti ccc-emile-antognetti added bug A bug report status: unverified Unverified labels Oct 24, 2022
@Jeckerson Jeckerson self-assigned this Oct 24, 2022
@Jeckerson Jeckerson added 5.0 The issues we want to solve in the 5.0 release status: high High and removed status: unverified Unverified labels Oct 24, 2022
@Jeckerson Jeckerson moved this to Backlog in Phalcon v5 Oct 24, 2022
@Jeckerson Jeckerson moved this from Backlog to In Progress in Phalcon v5 Oct 24, 2022
@Jeckerson Jeckerson linked a pull request Oct 24, 2022 that will close this issue
5 tasks
Jeckerson added a commit that referenced this issue Oct 24, 2022
Jeckerson added a commit that referenced this issue Oct 24, 2022
Jeckerson added a commit that referenced this issue Oct 24, 2022
Jeckerson added a commit that referenced this issue Oct 24, 2022
Jeckerson added a commit that referenced this issue Oct 24, 2022
Jeckerson added a commit that referenced this issue Oct 24, 2022
#16177 - Unify args of `fetchAll()` with `PDOStatement::fetchAll()`
@niden
Copy link
Member

niden commented Oct 24, 2022

Resolved in #16178

Thank you @ccc-emile-antognetti

@niden niden closed this as completed Oct 24, 2022
Repository owner moved this from In Progress to Implemented in Phalcon v5 Oct 24, 2022
@niden niden moved this from Implemented to Released in Phalcon v5 Oct 24, 2022
@ccc-emile-antognetti
Copy link
Author

Thanks so much for the speedy turnaround!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
5.0 The issues we want to solve in the 5.0 release bug A bug report status: high High
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants