Issues

ZF-1911: Making Zend_Db_Table_Rowset seekable to be able to fetch a specific Row

Description

Zend_Db_Table_Rowset_Abstract could implement SeekableIterator, instead of Iterator. That way, we could jump from result (Zend_Db_Table_Row) to result in the Rowset container.

We could as well fetch a specific result by its position, see :

Zend_Db_Table_Rowset_Abstract.php


<?php
abstract class Zend_Db_Table_Rowset_Abstract implements SeekableIterator, Countable
{
// ... all the actual code ...
/**
     * Take the Iterator to position $position
     * Required by interface SeekableIterator.
     *
     * @return void
     * @throws OutOfBoundsException
     */

    public function seek($position)
    {
        $position = (int)$position;
        if ($position >= 0 && $position < $this->_count) {
            $this->_pointer = $position;
        }else{
            throw new OutOfBoundsException("Illegal index '$position'");
        }
    }

    /**
     * get a specific Zend_Db_Table_Row in the Iterator
     *
     * @return Zend_Db_Table_Row
     * @throws Zend_Db_Table_Rowset_Exception
     */
    public function getRow($position)
    {
        $position = (int)$position;
        try{
            $this->seek($position);
        }catch (OutOfBoundsException $e){
            throw new Zend_Db_Table_Rowset_Exception("No row could be found at position $position");
        }
        return $this->current();
    }

Zend_Db_Table_Rowset_Exception.php


<?php
require_once 'Zend/Db/Table/Exception.php';

class Zend_Db_Table_Rowset_Exception extends Zend_Db_Table_Exception
{
}

Seek use-case :


<?php
// ... all DB code ...
$a = new AClassUsingZendDbTable();
$rowset = $a->fetchAll();
$a->seek(2);
// internal rowset pointer is now set to 2
$rowNumber2 = $a->current(); // $rowNumber2 is an instance of Zend_Db_Table_Row

getRow() use-case :


<?php
// ... all DB code ...
$a = new AClassUsingZendDbTable();
$rowset = $a->fetchAll();
$rowNumber3 = $a->getRow(3); // $rowNumber3 is an instance of Zend_Db_Table_Row

Comments

Assigned to Bill

done in r8099

julien: Please keep your code consistent with the ZF coding rules !!

Also it is not enough just to add the code... You should also add a small description in the manual to describe the usage and benefit.

Thank you