When using setFetchMode(\PDO::FETCH_CLASS, 'Classtype') and fetchAll(), the returned type is array<\stdClass> because fetchAll() will ignore the previously set fetchClassName.
The expected behaviour is that fetchAll() returns array<Classtype> as set previously with setFetchMode().
Test case
class Classtype
{
public int $id;
}
$sql = "SELECT 1 as id FROM dual";
/*
Working test, $result is array<Classtype>
*/
$stmt = $pdo->query($sql);
$result = $stmt->fetchAll(\PDO::FETCH_CLASS, 'Classtype');
var_dump($result);
/*
array(1) {
[0]=>
object(Classtype)#5 (1) {
["id"]=>
int(1)
}
}
*/
/*
Failing test, $result is array<stdClass>
*/
$stmt = $pdo->query($sql);
$stmt->setFetchMode(\PDO::FETCH_CLASS, 'Classtype');
$result = $stmt->fetchAll();
var_dump($result);
/*
array(1) {
[0]=>
object(stdClass)#6 (1) {
["id"]=>
string(1) "1"
}
}
*/
Summary
When using
setFetchMode(\PDO::FETCH_CLASS, 'Classtype')
andfetchAll()
, the returned type isarray<\stdClass>
becausefetchAll()
will ignore the previously setfetchClassName
.The expected behaviour is that
fetchAll()
returnsarray<Classtype>
as set previously withsetFetchMode()
.Test case
System details