pg_fetch_object bind parameters before call constructor.
| Request #71820 | pg_fetch_object bind parameters before call constructor. | ||||
|---|---|---|---|---|---|
| Submitted: | 2016-03-14 11:27 UTC | Modified: | 2016-03-15 14:44 UTC | ||
| From: | milan dot matejcek at gmail dot com | Assigned: | ab (profile) | ||
| Status: | Closed | Package: | PostgreSQL related | ||
| PHP Version: | 5.6.19 | OS: | ALL | ||
| Private report: | No | CVE-ID: | None | ||
[2016-03-14 11:27 UTC] milan dot matejcek at gmail dot com
Description: ------------ Hello, I want use pg_fetch_object function for bind data to object. In prototype of this function is fourth parameter like list of parameters for constructor. I add object whose care about conversion data type like string to \Datetime etc... But contructor is called after bind values and I cannot use my CoolObject. I found this https://github.com/php/php-src/blob/f57c0b324912ae3fce7940b836e35ca368cbd0ad/ext/pgsql/pgsql.c#L2817 where is first bind values and after call constructor but does not make sence. Then I can use simple pg_fetch_row and write my factory for object. Best regards Milan Test script: --------------- <?php $connectParameters = [ 'user' => '', 'password' => '', 'dbname' => 'test', 'host' => 'localhost', 'port' => 5432 ]; $tableName = 'test_pg_fetch_object'; class CoolObject { // this do anything with data public function stringToDatetime($value) { if ($this->isStringRepresentDatetime($value)) { return new \DateTime($value); } return $value; } private function isStringRepresentDatetime($value) { return FALSE; } } class TestRow { private $coolObject; private $data; public function __construct($coolObject) { // constructor is called after bind values $this->coolObject = $coolObject; } public function __set($name, $value) { var_dump($this->coolObject); // NULL why? I expect CoolObject. if (!isset($this->data[$name])) { return $this->data[$name] = $this->coolObject->stringToDatetime($value); } throw new \Exception('Duplicity column name.'); } } $connectStr = ''; foreach ($connectParameters as $k => $v) { $connectStr .= $k . '=' . $v . ' '; } $connection = pg_connect($connectStr); if (!$connection) { die('Connection faild.'); } $table = <<<SQL CREATE TABLE IF NOT EXISTS $tableName ( id serial NOT NULL, name character varying NOT NULL ); SQL; pg_query($connection, $table); pg_query_params('INSERT INTO ' . $tableName . ' (name) VALUES ($1), ($2);', ['$1' => 'Doe', '$2' => 'Joe']); $result = pg_query('SELECT * FROM ' . $tableName . ' LIMIT 10;'); while ($row = pg_fetch_object($result, NULL, 'TestRow', [new CoolObject()])) { var_dump($row); } Expected result: ---------------- Is in test script, what I expect.
Patches
Pull Requests
History
AllCommentsChangesGit/SVN commits
[2016-03-14 11:41 UTC] milan dot matejcek at gmail dot com
-Package: PDO PgSQL +Package: PostgreSQL related
[2016-03-14 11:41 UTC] milan dot matejcek at gmail dot com
[2016-03-15 14:44 UTC] ab@php.net
-Assigned To: +Assigned To: ab
[2016-03-17 11:50 UTC] ab@php.net
-Status: Assigned +Status: Closed