Re: DB_DataObject fetch methods
| From: | Mark Mitchenall | Date: | Wed, 10 Mar 2004 21:13:47 +0000 |
| Subject: | Re: DB_DataObject fetch methods | ||
| References: | 1 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-26264@lists.php.net to get a copy of this message | ||
Alex Hayes wrote:
$person= DB_DataObject::factory('person'); $person->hair = 'green'; $person->selectAdd(); $person->selectAdd('id,name'); $person->find(); while($person->fetch()) {I think you're right, which is why we wrote this simple class, but I don't think it's very PEAR compliant at the moment. It was just something we knocked up quickly, but you're welcome to use it... <?php require_once 'DB/DataObject.php' ; /** * CLASS: DataSelection * ==================== * * Class for creating arrays of DB_DataObjects based on simple * queries, SQL statements or existing dataobjects. * * @author Mark Mitchenall <mark@standingwave.co.uk> * @version $Id: dataselection.php,v 1.3 2004/03/06 00:52:27 mitch Exp $ * **/ class DataSelection { /** * DataSelection::fetch * * Fetch a selection of dataobjects based on a search field, * search value and orderBy. The field(s) to use as a key in * the returned array can also be specified, otherwise the * primary key for the dataobject will be used. * * If just the table name is passed, the method returns all * the records for that table. * * @param string Name of the table * @param string field to search * @param mixed value to search for * @param string field to sort the records on * @param mixed field(s) to use as a key * @return array dataobjects **/ function fetch ($table, $searchField=NULL, $searchValue=NULL, $orderBy=NULL, $useAsKey=NULL) { $dataobject = DB_DataObject::factory($table) ; if($searchField !== NULL) { $dataobject->$searchField = $searchValue ; } if($orderBy !== NULL) { $dataobject->orderBy ($orderBy) ; } $dataobject->find () ; return DataSelection::fetch_records($dataobject, $useAsKey) ; } /** * DataSelection::query * * Fetch a selection of dataobjects based on a SQL statement. * * The field(s) to use as a key in the returned array can also * be specified, otherwise the primary key(s) for the dataobject * will be used. * * @param string Name of the table * @param string SQL statement * @param mixed field(s) to use as a key * @return array dataobjects **/ function query ($table, $sql, $useAsKey=NULL) { $dataobject = DB_DataObject::factory($table) ; $dataobject->query ($sql) ; return DataSelection::fetch_records($dataobject, $useAsKey) ; } /** * DataSelection::fetch_records * * Fetches the records from the dataobject and returns an * array of them. * * @param object Dataobject to be used to fetch the records * @param mixed field(s) to use as a key * @return array dataobjects **/ function fetch_records ($dataobject, $useAsKey) { if($useAsKey !== NULL) { if (is_array ($useAsKey)) { $keys = $useAsKey ; } else { $keys = array ($useAsKey) ; } } else { $keys = $dataobject->keys() ; } $records = array () ; while($dataobject->fetch()) { if(count($keys) > 1) { $keyValues = array () ; foreach ($keys as $keyfield) { $keyValues[] = $dataobject->$keyfield ; } $key = implode ('^', $keyValues) ; } else { $key = $dataobject->$keys[0] ; } $records[$key] = $dataobject->__clone () ; } return $records ; } } ?> HTH Mark -- Mark Mitchenall, Standingwave Ltd (Complete Hosting and Development Services) Tel := +44 (0)20 8452 3031 Email := mark@standingwave.co.uk mark@mitchenall.com Home := http://www.standingwave.co.uk http://www.mitchenall.com$result[] = $person->toArray();} So, ive only been looking at this for about an hour, so please correct me if im wrong.