Req #71820 [NEW]: pg_fetch_object bind parameters before call constructor.
| From: | milan dot matejcek at gmail dot com | Date: | Mon, 14 Mar 2016 11:27:48 +0000 |
| Subject: | Req #71820 [NEW]: pg_fetch_object bind parameters before call constructor. | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-199810@lists.php.net to get a copy of this message | ||
From: milan dot matejcek at gmail dot com
Operating system: ALL
PHP version: 5.6.19
Package: PDO PgSQL
Bug Type: Feature/Change Request
Bug description:pg_fetch_object bind parameters before call constructor.
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 bug report at https://bugs.php.net/bug.php?id=71820&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=71820&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=71820&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=71820&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=71820&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=71820&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=71820&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=71820&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=71820&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=71820&r=support
Expected behavior: https://bugs.php.net/fix.php?id=71820&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=71820&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=71820&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=71820&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=71820&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=71820&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=71820&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=71820&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=71820&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=71820&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=71820&r=mysqlcfg