php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
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 commitsRelated reports
 [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
Change package. Not PDO, but postgreSql functions.
 [2016-03-15 14:44 UTC] ab@php.net
-Assigned To: +Assigned To: ab
 [2016-03-17 11:50 UTC] ab@php.net
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
 [2016-03-17 11:50 UTC] ab@php.net
-Status: Assigned +Status: Closed
 [2016-05-09 15:36 UTC] ab@php.net
Automatic comment on behalf of ab
Revision: http://git.php.net/?p=php-src.git;a=commit;h=fc2a45b70a6675cad627cfe6341dc77d61952ba4
Log: Revert &quot;Fixed bug #71820 pg_fetch_object bind parameters before call constructor&quot;
 [2016-07-20 11:31 UTC] davey@php.net
Automatic comment on behalf of ab
Revision: http://git.php.net/?p=php-src.git;a=commit;h=fc2a45b70a6675cad627cfe6341dc77d61952ba4
Log: Revert &quot;Fixed bug #71820 pg_fetch_object bind parameters before call constructor&quot;
 [2016-07-20 11:32 UTC] davey@php.net
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
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Oct 06 19:01:27 2024 UTC