php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #73397 Add FETCH_CLASS_VIA_CONSTRUCTOR fetch mode
Submitted: 2016-10-26 20:58 UTC Modified: -
Votes:2
Avg. Score:4.0 ± 1.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: tomas dot fejfar at gmail dot com Assigned:
Status: Open Package: PDO related
PHP Version: Irrelevant OS: N/A
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: tomas dot fejfar at gmail dot com
New email:
PHP Version: OS:

 

 [2016-10-26 20:58 UTC] tomas dot fejfar at gmail dot com
Description:
------------
PDO allows property injection, but not creation via constructor. That forces the developer to do extra work to ensure that the created object is not in inconsistent state. If there was FETCH_CLASS_VIA_CONSTRUCTOR fetch mode PDO would be much closer to being a very simple ORM and much easier to use for people not using a special DB framework. 
Not that I currently absolutely need it because there is workaround, but I think it would be a nice addition. 



Expected result:
----------------
$results = $stms->fetchAll(PDO::FETCH_CLASS_VIA_CONSTRUCTOR, Task::class);

Actual result:
--------------
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  $results[] = new Task($row['id'], $row['name']);
}

//or

$stmt->fetchAll(PDO::FETCH_FUNC, function ($id, $name) { return new Task($id, $name); });

Patches

Add a Patch

Pull Requests

Add a Pull Request

 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Tue Nov 19 11:01:30 2019 UTC