Details
-
Type:
Bug
-
Status:
Resolved
-
Priority:
Major
-
Resolution: Duplicate
-
Affects Version/s: 1.5.2
-
Fix Version/s: 1.8.2
-
Component/s: Zend_Db_Table
-
Labels:None
-
Fix Version Priority:Should Have
Description
Because of the problem described in ZF-2546, joins will fail on a Zend_Db_Table_Select:
product-category.php
$categoryTable = new CategoryTable(array( 'db' => 'db', 'name' => 'category', 'primary' => 'id')); // Get categories belonging to a product-- // A many-to-many relationship $select = $categoryTable->select() ->join('category_product', 'category_product.category_id = category.id', array()) ->where('category_product.product_id=?', 123); echo "\$select: ", $select, "\n"; // SELECT `category`.* FROM `category_product` // INNER JOIN `category` WHERE (category_product.product_id='123') // WRONG: // 1) Incorrect table in 'FROM' // 2) Missing 'ON' clause in 'INNER JOIN' // The workaround: // This time, include a from() $select = $categoryTable->select() ->from('category') ->join('category_product', 'category_product.category_id = category.id', array()) ->where('category_product.product_id=?', 123); echo "\$select: ", $select, "\n"; // SELECT `category`.* FROM `category` // INNER JOIN `category_product` ON category_product.category_id=category.id // WHERE (category_product.product_id='123') // *CORRECT*
Issue Links
| This issue depends on: | ||||
| ZF-2546 | Zend_Db_Table_Select should include its own table in the FROM section |
|
|
|
This was fixed in part by the feature added in
ZF-2798.To make your join queries work, use this logic:
$table->select(Zend_Db_Table_Abstract::SELECT_WITH_FROM_PART)->setIntegrityCheck(false);
The documentation was also updated.
ZF-2798. To make your join queries work, use this logic: $table->select(Zend_Db_Table_Abstract::SELECT_WITH_FROM_PART)->setIntegrityCheck(false); The documentation was also updated.