Req #71820 [Opn]: pg_fetch_object bind parameters before call constructor.

From: Date: Mon, 14 Mar 2016 11:41:10 +0000
Subject: Req #71820 [Opn]: pg_fetch_object bind parameters before call constructor.
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-199811@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=71820&edit=1 ID: 71820 User updated by: milan dot matejcek at gmail dot com Reported by: milan dot matejcek at gmail dot com Summary: pg_fetch_object bind parameters before call constructor. Status: Open Type: Feature/Change Request -Package: PDO PgSQL +Package: PostgreSQL related Operating System: ALL PHP Version: 5.6.19 Block user comment: N Private report: N New Comment: Change package. Not PDO, but postgreSql functions. Previous Comments: ------------------------------------------------------------------------ [2016-03-14 11:27:43] 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. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=71820&edit=1

« previous php.bugs (#199811) next »