cvs: pear /Structures_DataGrid/DataGrid/DataSource DB.php DataObject.php

From: Date: Wed, 18 Aug 2004 18:48:54 +0000
Subject: cvs: pear /Structures_DataGrid/DataGrid/DataSource DB.php DataObject.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-23084@lists.php.net to get a copy of this message
asnagy Wed Aug 18 14:48:54 2004 EDT Modified files: /pear/Structures_DataGrid/DataGrid/DataSource DB.php DataObject.php Log: Removed limit and sort method Fetch now handles sorting and limiting

http://cvs.php.net/diff.php/pear/Structures_DataGrid/DataGrid/DataSource/DB.php?r1=1.4&r2=1.5&ty=u Index: pear/Structures_DataGrid/DataGrid/DataSource/DB.php diff -u pear/Structures_DataGrid/DataGrid/DataSource/DB.php:1.4 pear/Structures_DataGrid/DataGrid/DataSource/DB.php:1.5 --- pear/Structures_DataGrid/DataGrid/DataSource/DB.php:1.4 Tue Aug 17 17:41:16 2004 +++ pear/Structures_DataGrid/DataGrid/DataSource/DB.php Wed Aug 18 14:48:53 2004 @@ -25,7 +25,7 @@ * * This class is a data source driver for the PEAR::DB::DB_Result object * - * @version $Revision: 1.4 $ + * @version $Revision: 1.5 $ * @author Andrew S. Nagy <asnagy@webitecture.org> * @access public * @package Structures_DataGrid @@ -41,9 +41,6 @@ */ var $_result; - var $_offset = 0; - var $_limit = null; - /** * Constructor * @@ -61,9 +58,16 @@ * @access public * @return mixed True on success, PEAR_Error on failure */ - function bind(&$result) + function bind(&$result, $options=array()) { - if (is_subclass_of($result, 'DB_Result')) { + if ($options) { + $test = $this->_setOptions($options); + if (PEAR::isError($test)) { + return $test; + } + } + + if (get_class($result) == 'db_result') { $this->_result =& $result; return true; } else { @@ -72,61 +76,60 @@ } /** - * Sort - * - * @access public - * @param string $field The field to sort by - * @param string $direction The direction to sort, either ASC or DESC - */ - function sort($field, $direction='ASC') - { - } - - /** - * Limit - * - * @access public - * @param int $offset The count offset - * @param int $length The amount to limit to - */ - function limit($offset, $length) - { - $this->_offset = $offset; - $this->_limit = $length; - } - - /** * Fetch * + * @param integer $offset Offset (starting from 0) + * @param integer $limit Limit + * @param string $sortField Field to sort by + * @param string $sortDir Sort direction : 'ASC' or 'DES * @access public * @return array The 2D Array of the records */ - function &fetch() + function &fetch($offset=0, $limit=null, $sortField=null, $sortDir='ASC') { $recordSet = array(); if ($numRows = $this->_result->numRows()) { - + + /* We need to fetch everything in order to sort. + * I comment that out : + // Move database pointer for limiting - if (($this->_offset > $this->_limit) && - ($numRows > $this->_offset)) { - for ($i = 0; $i < $this->_offset; $i++) { + if (($offset > $limit) && + ($numRows > $offset)) { + for ($i = 0; $i < $offset; $i++) { $this->_result->fetchRow(); } } + */ // Fetch Records $i = 0; - while (($record = $this->_result->fetchRow(DB_FETCHMODE_ASSOC)) && - ($i < $this->_limit)) { + + + // Same thing as above. + // + // By the way if you use 'and' instead of '&&' you don't need to put + // so many parenthesis. Operator precedence is : '&&' > '=' > 'and' + // + // while (($record = $this->_result->fetchRow(DB_FETCHMODE_ASSOC)) && + // ($i < $limit)) { + // + while ($record = $this->_result->fetchRow(DB_FETCHMODE_ASSOC)) { $recordSet[] = $record; - $i++ + $i++; } } else { + // FIXME: This is no error (you have to return an empty set) : return new PEAR_Error('No records found'); } - return array('Records' => $recordSet); + // sort and limit the recordSet (nifty he ?) : + $recordSet =& Structures_DataGrid_DataSource_Array::staticFetch( + $recordSet, $this->_options['fields'], $offset, + $limit, $sortField, $sortDir); + + return $recordSet; } /** http://cvs.php.net/diff.php/pear/Structures_DataGrid/DataGrid/DataSource/DataObject.php?r1=1.3&r2=1.4&ty=u Index: pear/Structures_DataGrid/DataGrid/DataSource/DataObject.php diff -u pear/Structures_DataGrid/DataGrid/DataSource/DataObject.php:1.3 pear/Structures_DataGrid/DataGrid/DataSource/DataObject.php:1.4 --- pear/Structures_DataGrid/DataGrid/DataSource/DataObject.php:1.3 Tue Aug 17 17:42:17 2004 +++ pear/Structures_DataGrid/DataGrid/DataSource/DataObject.php Wed Aug 18 14:48:53 2004 @@ -26,7 +26,19 @@ * * This class is a data source driver for a PEAR::DB::DB_DataObject object * - * @version $Revision: 1.3 $ + * Recognized options : + * + * <b>"labels_property" : </b> The name of a property that you can set + * within your DataObject. This property is expected to contain the + * same kind of information as the "labels" options. If the "labels" + * option is set, this one will not be used. Default : "fb_fieldsLabels". + * + * <b>"fields_property" : </b> The name of a property that you can set + * within your DataObject. This property is expected to contain the + * same kind of information as the "fields" options. If the "fields" + * option is set, this one will not be used. Default : "fb_fieldsToRender". + * + * @version $Revision: 1.4 $ * @author Olivier Guilyardi <olivier@samalyse.com> * @author Andrew Nagy <asnagy@webitecture.org> * @access public @@ -53,24 +65,60 @@ function Structures_DataGrid_DataSource_DataObject() { parent::Structures_DataGrid_DataSource(); - $this->_addDefaultOptions(array('generate_columns' => true, - 'labels_property' => - 'fb_fieldLabels', - 'render_property' => - 'fb_fieldsToRender')); + $this->_addDefaultOptions(array( + 'labels_property' => 'fb_fieldLabels', + 'fields_property' => 'fb_fieldsToRender')); } /** * Bind * - * @param object DB_DataObject The DB_DataObject object to bind + * @param object DB_DataObject $dataobject The DB_DataObject object + * to bind + * @param array $options Associative array of + * options. * @access public * @return mixed True on success, PEAR_Error on failure */ - function bind(&$dataobject) + function bind(&$dataobject, $options=array()) { + if ($options) { + $test = $this->_setOptions($options); + if (PEAR::isError($test)) { + return $test; + } + } + if (is_subclass_of($dataobject, 'DB_DataObject')) { $this->_dataobject =& $dataobject; + + $mergeOptions = array(); + + // Merging the fields and fields_property options + if (!$this->_options['fields']) { + if ($fieldsVar = $this->_options['fields_property'] + and isset($this->_dataobject->$fieldsVar)) { + + $mergeOptions['fields'] = $this->_dataobject->$fieldsVar; + } else { + $mergeOptions['fields'] = + array_keys($this->_dataobject->toArray()); + } + } + + // Merging the labels and labels_property options + if (!$this->_options['labels'] + and $labelsVar = $this->_options['labels_property'] + and isset($this->_dataobject->$labelsVar)) { + + $mergeOptions['labels'] = $this->_dataobject->$labelsVar; + + } + + if ($mergeOptions) { + $this->_setOptions($mergeOptions); + } + return true; } else { return new PEAR_Error('The provided source must be a DB_DataObject'); @@ -78,83 +126,54 @@ } /** - * Sort - * - * @access public - * @param string $field The field to sort by - * @param string $direction The direction to sort, either ASC or DESC - */ - function sort($field, $direction='ASC') - { - $this->_dataobject->orderBy("$field $direction"); - } - - /** - * Limit - * - * @access public - * @param int $offset The count offset - * @param int $length The amount to limit to - */ - function limit($offset, $length) - { - if ($offset) { - $this->_dataobject->limit($offset, $length); - } else { - $this->_dataobject->limit($length); - } - } - - /** * Fetch * + * @param integer $offset Limit offset (starting from 0) + * @param integer $len Limit length + * @param string $sortField Field to sort by + * @param string $sortDir Sort direction : 'ASC' or 'DESC' * @access public - * @return array The 2D Array of the records + * @return array The 2D Array of the records */ - function &fetch() + function &fetch($offset=0, $len=null, $sortField=null, $sortDir='ASC') { - $columns = array(); - $records = array(); - - /* Auto generating columns if required */ - if ($this->_options['generate_columns']) { - - if ($fRender = $this->_options['render_property']) { - $fList = @$this->_dataobject->$fRender; - } - - if (!$fList) { - $fList = array_keys($this->_dataobject->toArray()); - } - - $labelVar = $this->_options['labels_property']; - $field2label = @$this->_dataobject->$labelVar - or $field2label = array(); - - include_once('Structures/DataGrid/Column.php'); - - foreach ($fList as $field) { - $label = strtr($field,$field2label); - $col = new Structures_DataGrid_Column($label, $field, $field); - $columns[] = $col; - } - + // Sorting + if ($sortField) { + $this->_dataobject->orderBy("$sortField $sortDir"); + } + + // Limiting + if ($ofs) { + $this->_dataobject->limit($ofs, $len); + } elseif ($len) { + $this->_dataobject->limit($len); } + + $records = array(); + // Retrieving data if ($this->_dataobject->find()) { while ($this->_dataobject->fetch()) { - $records[] = $this->_dataobject->toArray(); + // Only retrieve the fields to render, as set in $fList : + $rec = array(); + foreach ($this->_options['fields'] as $fName) { + $rec[$fName] = $this->_dataobject->$fName; + } + $records[] = $rec; } } else { return new PEAR_Error('Couldn\'t fetch data'); } - return array('Columns' => $columns, 'Records' => $records); + return $records; } /** * Count * + * NOTE: This has to be called before fetch() !! + * This may be a DataObject bug. See test_dataobject_options.php. + * * @access public * @return int The number or records */ @@ -162,7 +181,6 @@ { return $this->_dataobject->count(); } - } ?>
« previous php.pear.cvs (#23084) next »