FAQ
Edit report at http://pear.php.net/bugs/bug.php?id=15315&edit=1

ID: 15315
Updated by: alan@akbkhome.com
Reported By: escapisam at gmail dot com
Summary: DB_DataObject::getLink() ignores current object's
database
-Status: Open
+Status: Suspended
Type: Bug
Package: DB_DataObject
Package Version: 1.8.8
PHP Version: 5.2.5
Roadmap Versions:
New Comment:

-Status: Open
+Status: Suspended
Needs patch.


Previous Comments:
------------------------------------------------------------------------

[2009-01-11 19:45:52] escapisam

I made a mistake - the result of select * from ... in each database is
wrong. Here is the correct output:

-- staging=> select * from team;
-- id | name
-- ----+------
-- 1 | Ants
-- 2 | Bees
-- (2 rows)
--
-- staging=> select * from person;
-- id | name | team_id
-- ----+------+---------
-- 1 | Bob | 1
-- (1 row)
--
-- \c production
--
-- production=> select * from team;
-- id | name
-- ----+--------
-- 1 | Lions
-- 2 | Tigers
-- (2 rows)
--
-- production=> select * from person;
-- id | name | team_id
-- ----+------+---------
-- 1 | Bob | 2
-- (1 row)

------------------------------------------------------------------------

[2009-01-11 19:42:26] escapisam

Test case. This is environment dependent so you'll have to create your
own databases and alter code accordingly.

$ psql -h myhost -d template1 -u dbuser

CREATE DATABASE staging;
CREATE DATABASE production;

\c staging;

CREATE TABLE team
(
id INT PRIMARY KEY,
name VARCHAR
);

CREATE TABLE person
(
id INT PRIMARY KEY,
name VARCHAR,
team_id INT REFERENCES team(id)
);

INSERT INTO team (id,name) VALUES (1,'Ants');
INSERT INTO team (id,name) VALUES (2,'Bees');
INSERT INTO person (id,name,team_id) VALUES (1,'Bob',1); -- Bob is in
team id=1 which in staging is the Ants team


\c production;

CREATE TABLE team
(
id INT PRIMARY KEY,
name VARCHAR
);

CREATE TABLE person
(
id INT PRIMARY KEY,
name VARCHAR,
team_id INT REFERENCES team(id)
);


INSERT INTO team (id,name) VALUES (1,'Lions');
INSERT INTO team (id,name) VALUES (2,'Tigers');
INSERT INTO person (id,name,team_id) VALUES (1,'Bob',2); -- Bob is in
team id=2 which in production is the Tigers team

-- staging=> select * from team;
-- id | name
-- ----+--------
-- 1 | Lions
-- (2 rows)
--
-- staging=> select * from person;
-- id | name | team_id
-- ----+------+---------
-- 1 | Bob | 1
-- (1 row)

-- production=> select * from team;
-- id | name
-- ----+--------
-- 1 | Tigers
-- (21 rows)
--
-- production=> select * from person;
-- id | name | team_id
-- ----+------+---------
-- 1 | Bob | 1
-- (1 row)


<?php

// Do your own include path

@define('DB_DATAOBJECT_NO_OVERLOAD', true);

require_once 'PEAR.php';
require_once 'DB/DataObject.php';


// Set up dsn for each database
global $_DB_DATAOBJECT;

$_DB_DATAOBJECT['CONFIG'] = array();
$_DB_DATAOBJECT['CONFIG']['database'] =
'pgsql://dbuser@myhost/staging'; // default
$_DB_DATAOBJECT['CONFIG']['database_production'] =
'pgsql://dbuser@myhost/production';



// Will Assume dataobjects have been created for team and person

// Create person object
$person_staging = DB_Dataobject::factory('Person');

// Don't set connection - will connect to default (staging) database

// get Bob from default database (staging) (id=1)
$person_staging->get(1);

// See what we've got
echo "Bob staging: {$person_staging->id} / {$person_staging->name} /
{$person_staging->team_id}<br />\n";

// Get Bob's team in default database
$bobs_team_staging = $person_staging->getLink('team_id');

echo "Bob's team staging: {$bobs_team_staging->id} /
{$bobs_team_staging->name}<br />\n"; // "1 / Ants" - ok



// Create person object
$person_prod = DB_Dataobject::factory('Person');

// Set to connect to production database
$person_prod->database('production');

// get Bob from production (id=1)
$person_prod->get(1);

// See what we've got
echo "Bob production: {$person_prod->id} / {$person_prod->name} /
{$person_prod->team_id}<br />\n";

// Get Bob's team in production
$bobs_team_prod = $person_prod->getLink('team_id');

echo "Bob's team production: {$bobs_team_prod->id} /
{$bobs_team_prod->name}<br />\n"; // "2 / Bees" (not ok, this came from
default database - should be "2 / Tigers" as in production).

?>

------------------------------------------------------------------------

[2008-12-27 00:29:23] doconnor

Got a small executable test case? This doesn't sound too tricky...

------------------------------------------------------------------------

[2008-12-16 00:03:57] escapisam

Description:
------------
When calling the method DB_DataObject::getLink(), the default global
database connection is used instead of the connection of the object the
method is being called from.

Possible cause:

In the method DB_DataObject::getLink(), these is a call to the
DB_DataObject::factory() method (DB_DataObject.php line 2909) which
creates an object of the link type being requested. However the database
connection is not set on the newly created object so it gets the default
global connection.

Using PostgreSQL 8.3.4 on i686-redhat-linux-gnu, PHP 5.2.5

------------------------------------------------------------------------


--
Edit this bug report at http://pear.php.net/bugs/bug.php?id=15315&edit=1

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppear-bugs @
categoriesphp
postedOct 9, '09 at 12:25a
activeOct 9, '09 at 12:25a
posts1
users1
websitepear.php.net

1 user in discussion

Alan: 1 post

People

Translate

site design / logo © 2022 Grokbase