Req #71820 [Asn->Csd]: pg_fetch_object bind parameters before call constructor.
| From: | ab@php.net | Date: | Thu, 17 Mar 2016 11:50:13 +0000 |
| Subject: | Req #71820 [Asn->Csd]: pg_fetch_object bind parameters before call constructor. | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-199888@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
Updated by: ab@php.net
Reported by: milan dot matejcek at gmail dot com
Summary: pg_fetch_object bind parameters before call
constructor.
-Status: Assigned
+Status: Closed
Type: Feature/Change Request
Package: PostgreSQL related
Operating System: ALL
PHP Version: 5.6.19
Assigned To: ab
Block user comment: N
Private report: N
New Comment:
Automatic comment on behalf of ab
Revision: http://git.php.net/?p=php-src.git;a=commit;h=b4eedd128ba9f61be08a50c94afd72837d7cf70b
Log: Fixed bug #71820 pg_fetch_object bind parameters before call constructor
Previous Comments:
------------------------------------------------------------------------
[2016-03-14 11:41:10] milan dot matejcek at gmail dot com
Change package. Not PDO, but postgreSql functions.
------------------------------------------------------------------------
[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