Zend Framework

Using subquery in from() is broken because of new schema logic

Details

  • Type: Bug Bug
  • Status: Resolved Resolved
  • Priority: Major Major
  • Resolution: Won't Fix
  • Affects Version/s: 1.0.1
  • Fix Version/s: 1.0.4
  • Component/s: Zend_Db_Select
  • Labels:
    None
  • Fix Version Priority:
    Should Have

Description

Using a subquery as the table argument in the from() call used to work fine, like so:

$select = $db->select();
$select->from("(select people.id from people)", "count(id)");

I know there are cleaner ways to get the same result, but this is a simplified version and in some cases it is necessary to do a select like this, for example when doing your own pagination you may want to put the sql from one query into a call like this to find out the number of rows returned from it.

When doing this, if the code in the table argument contains a period, the new schema code on line 358 in Select.php will break the query:

// Schema from table name overrides schema argument
if (false !== strpos($tableName, '.')) { list($schema, $tableName) = explode('.', $tableName); }

Activity

Hide
Thomas Weidner added a comment -

Assigned to Bill

Show
Thomas Weidner added a comment - Assigned to Bill
Hide
julien PAULI added a comment -

Same behavior with subselect queries with Zend_Db_Select :

$db->select()
->from ('table1', array('t1_label'))
->joinInner(
      array('T2' => new Zend_Db_Expr (
         '('.
         $db->select()
         ->from('table2', array('t2_label'))
         ->where('condition')
         .')'
      )),
      'table1.t2_id = T2.t2_id',
      array('t2_label')
)

breaks it as well :

`(SELECT
``table2```.```t2_label``
FROM ``table2``
WHERE condition)`

patch could be :

if (!$tableName instanceof Zend_Db_Expr && false !== strpos($tableName, '.')) {
            list($schema, $tableName) = explode('.', $tableName);
        }
Show
julien PAULI added a comment - Same behavior with subselect queries with Zend_Db_Select :
$db->select()
->from ('table1', array('t1_label'))
->joinInner(
      array('T2' => new Zend_Db_Expr (
         '('.
         $db->select()
         ->from('table2', array('t2_label'))
         ->where('condition')
         .')'
      )),
      'table1.t2_id = T2.t2_id',
      array('t2_label')
)
breaks it as well : `(SELECT ``table2```.```t2_label`` FROM ``table2`` WHERE condition)` patch could be :
if (!$tableName instanceof Zend_Db_Expr && false !== strpos($tableName, '.')) {
            list($schema, $tableName) = explode('.', $tableName);
        }
Hide
Simon Mundy added a comment -

This use-case can be avoided by using the Zend_Db_Expr object to enclose the subquery.

Show
Simon Mundy added a comment - This use-case can be avoided by using the Zend_Db_Expr object to enclose the subquery.

People

Vote (0)
Watch (2)

Dates

  • Created:
    Updated:
    Resolved:

Time Tracking

Estimated:
1d
Original Estimate - 1 day
Remaining:
1d
Remaining Estimate - 1 day
Logged:
Not Specified
Time Spent - Not Specified