PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » Database » MDB_QueryTool » Bug #8292

Joining tables where one table name occurs in another table name

Details

Submitted2006-07-26 07:18 UTC
Fromtbarsness at gmail dot com
Assignedquipo
StatusNo Feedback
PackageMDB_QueryTool
PHP Version4.4.2
OSWindows XP, Operating System Ind
Roadmaps(Not assigned)

Comments

[2006-07-26 07:18 UTC] tbarsness at gmail dot com

Description:
------------
I have run into a problem with this package in the _buildWhere function in Query.php when joining tables where one table name, and the containing table name has the contained table name preceded and succeded by an underscore.

Say I am joining tables `category` and `xref_catecory_item`.

I get a problem around line 1885.

In the where clause, the table name ends up looking like xrefcategory.super_category, which isn't what I want at all.

This looks like it is a feature based on the comments around these lines of code, but is the only way around this to rename my table?

Test script:
---------------
<?php
require_once 'MDB/QueryTool.php';
define('TABLE_CARS', 'cars');
define('TABLE_XREF_CAR_DRIVER', 'xref_car_driver');
$dsn = 'mysql://user:pass@host/dbname';

/**
* Let's suppose the "car" table has the following fields:
* (id, model, hp, color, clima, price)
*/
class Car extends MDB_QueryTool
{
var $table = TABLE_CARS;
var $sequenceName = TABLE_CARS;

// this is default, but to demonstrate it here ...
var $primaryCol = 'id';

/**
* This table spec assigns a short name to a table name
* this short name is needed in case the table name changes
* i.e. when u put the application on a provider's db, where you have to
* prefix each table, and you dont need to change the entire application to
* where you refer to joined table columns, for that joined results the
* short name is used instead of the table name
*/
var $tableSpec = array(
array('name' => TABLE_CARS, 'shortName' => 'cars')
);
}

$car = new Car($dsn);
$car->setSelect('*');
$car->addJoin(TABLE_XREF_CAR_DRIVER, '`'.TABLE_CAR.'`.`id_car` = `'.TABLE_XREF_CAR_DRIVER.'`.`id_car`');
echo $car->getQueryString();

Expected result:
----------------
SELECT * FROM car, xref_car_driver
WHERE `car`.`id_car` = `xref_car_driver`.`id_car`

Actual result:
--------------
SELECT * FROM car, xref_car_driver
WHERE `car`.`id_car` = `xrefcar.driver`.`id_car`