Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 1.1.2

Bug #945 [Patch] Return result object
Submitted: 2004-03-04 11:48 UTC
From: dostick at ctco dot lv Assigned: quipo
Status: Closed Package: DB_QueryTool
PHP Version: 4.3.4 OS: UNIX
Roadmaps: (Not assigned)    
Subscription  


 [2004-03-04 11:48 UTC] dostick at ctco dot lv
Description: ------------ This is extension to make QueryTool return object result, just like PEAR_DB. This extension allow to transparently switch from PEAR_DB to QueryTool. <?php // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2003 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.02 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Author: Roman Dostovalov, Com-tec-so S.A. <roman.dostovalov@ctco.lv> // +----------------------------------------------------------------------+ // // $Id: ResultObject.php,v 1.1 2004/03/04 11:40:32 dostick Exp $ // /** * Include parent class */ require_once 'DB/QueryTool/Result.php'; /** * Result row class */ Class DB_QueryTool_Result_Row { /** * create object properties form array * */ function DB_QueryTool_Result_Row($arr) { foreach ($arr as $key=>$value) { $this->$key = $value; } } // end func } /** * oh. * * @access public */ class DB_QueryTool_Result_Object extends DB_QueryTool_Result { /** * Emulates PEAR DB numrows * * @param * @access public * @return void */ function numRows() { return $this->getCount(); } // end func /** * This function emulates PEAR_DB function FetchRow * With this function DB_QueryTool can transparently replace PEAR_DB * * @todo implement fetchmode support? * @access public * @return void */ function fetchRow($fetchmode) { $arr=$this->getNext(); if (!PEAR::isError($arr)) { $row=new DB_QueryTool_Result_Row($arr); return $row; } return false; } // end func /** * Wrapper for getNext function * * @access public * @return void */ function getNext() { if (!is_array($this->_dataKeys)) { $this->_dataKeys = array_keys($this->_data); } return parent::getNext(); } // end func } // end class ?> Added functions to Query.php: /** * * * @version 2004/04/04 * @access public * @author Roman Dostovalov <roman.dostovalov@ctco.lv> * @param * @return */ function useResultObject( $doit=true ) { $this->_useResultObject = $doit; if( $doit ) require_once 'DB/QueryTool/ResultObject.php'; } Modified function in Query.php: /** * * * @version 2004/04/04 * @access public * @author Wolfram Kriesing <wk@visionp.de> * @param * @return */ function returnResult( &$result ) { if( $this->_useResult ) { if( $result==false ) return false; return new DB_QueryTool_Result( $result ); } if( $this->_useResultObject ) { if( $result==false ) return false; return new DB_QueryTool_Result_Object( $result ); } return $result; } Reproduce code: --------------- Tutorial: Before QueryTool: $db=DB::connect(....); $query = "SELECT * FROM users WHERE users.level=3"; $result = $db->query($query); if ($result->numRows()) { while ($user = $result->fetchRow()) { echo $user->name; } } With QueryTool: $db=DB::connect(....); $q=new DB_Query_Tool; $q->setDbInstance($db); $q->useResultObject(true); $q->setWhere('users.level=3'); $q->getAll(); And the rest of your code is unchanged! it still works with PEAR_DB -style object methods.

Comments

 [2004-04-27 19:41 UTC] quipo
Since I haven't received any reply to my private mail, I post my questions regarding this patch here. After a look at the patch, there are some things I don't understand: 1) DB_QueryTool is supposed to be extended by custom classes. From the example you gave, it looks like you want to use it without subclassing it? 2) what's the point of the DB_QueryTool_Result_Row class? It looks like wrapping an array into an object is just unnecessary overhead 3) what's the relation between your DB_QueryTool_Result_Object and DB_Result? 4) what's the real benefit that the pkg will gain with your patch? Thanks for your answers Lorenzo Alberton
 [2004-04-27 22:24 UTC] quipo
OK, I think I understood. You are probably using DB with fetchMode=DB_FETCHMODE_OBJECT. It was quite an important piece of information... ;-)
 [2004-04-27 23:05 UTC] quipo
This bug has been fixed in CVS. In case this was a documentation problem, the fix will show up at the end of next Sunday (CET) on pear.php.net. In case this was a pear.php.net website problem, the change will show up on the website in short time. Thank you for the report, and for helping us make PEAR better. Ok, I've integrated it in DB_DataObject, and fixed some other problems I found in DB_DataObject_Result.