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

Add support for more PDOStatement fetch modes #2553

Merged
merged 1 commit into from
Jan 6, 2020
Merged

Conversation

duskwuff
Copy link
Contributor

@duskwuff duskwuff commented Jan 5, 2020

The PDO::FETCH_CLASSTYPE and PDO::FETCH_PROPS_LATE flags aren't supported yet, as implementing them would require some way to read the value of an expression like PDO::FETCH_CLASS | PDO::FETCH_CLASSTYPE as the argument to PDOStatement::fetch(). This seems like something that might be useful down the road, but I'm hesitant to implement it just for this feature. :)

There are a few other really obscure fetch modes which aren't supported either, like PDO::FETCH_FUNC and PDO::FETCH_SERIALIZE. They're barely even documented and I'm not sure they even make sense to use outside of fetchAll(), so... whatever.

(in re. #2529)

Flags like PDO::FETCH_CLASSTYPE and PDO::FETCH_PROPS_LATE aren't
supported yet.

(in re. vimeo#2529)
@muglug muglug merged commit 63dea52 into vimeo:master Jan 6, 2020
@muglug
Copy link
Collaborator

muglug commented Jan 6, 2020

Thanks!

I think PDO::FETCH_CLASS can use information from the second parameter, right?

@staabm
Copy link
Contributor

staabm commented Jan 6, 2020

Thanks!

I think PDO::FETCH_CLASS can use information from the second parameter, right?

Nope, I dont think so:
https://www.php.net/manual/en/pdostatement.fetch.php

PDO::FETCH_CLASS: returns a new instance of the requested class, mapping the columns of the result set to named properties in the class, and calling the constructor afterwards, unless PDO::FETCH_PROPS_LATE is also given. If fetch_style includes PDO::FETCH_CLASSTYPE (e.g. PDO::FETCH_CLASS | PDO::FETCH_CLASSTYPE) then the name of the class is determined from a value of the first column.

I guess you mixed it up with fetchObject()
https://www.php.net/manual/en/pdostatement.fetchobject.php

@muglug
Copy link
Collaborator

muglug commented Jan 6, 2020

Ahh right yes, sorry.

@MarkusRodler
Copy link

The PDO::FETCH_CLASSTYPE and PDO::FETCH_PROPS_LATE flags aren't supported yet, as implementing them would require some way to read the value of an expression like PDO::FETCH_CLASS | PDO::FETCH_CLASSTYPE as the argument to PDOStatement::fetch(). This seems like something that might be useful down the road, but I'm hesitant to implement it just for this feature. :)

There are a few other really obscure fetch modes which aren't supported either, like PDO::FETCH_FUNC and PDO::FETCH_SERIALIZE. They're barely even documented and I'm not sure they even make sense to use outside of fetchAll(), so... whatever.

(in re. #2529)

I don't think that PDO::FETCH_FUNC is very obscure. It is acutally a really handy one. For example if you want to initialize a class with a private constructor. Then you can use FETCH_FUNC to call your static factory method instead.

Example:

// Repository.php
$query->fetchAll(PDO::FETCH_FUNC, [ValueObject::class, 'createFromDb']); // Will return an instance of the ValueObject class
// ValueObject.php
class ValueObject
{
    private function __construct(DateTimeImmutable $test) {}
    public static function createFromDb(string $test): self
    {
        return new self(new DateTimeImmutable($test));
    }
}

It would be very nice if psalm could support this case, too.

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.

4 participants