Zend Framework

Making Zend_Db_Table_Rowset seekable to be able to fetch a specific Row

Details

  • Type: Improvement Improvement
  • Status: Resolved Resolved
  • Priority: Minor Minor
  • Resolution: Fixed
  • Affects Version/s: None
  • Fix Version/s: 1.0.4
  • Component/s: Zend_Db_Table
  • Labels:
    None
  • Fix Version Priority:
    Nice to Have

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

Activity

Hide
Thomas Weidner added a comment -

Assigned to Bill

Show
Thomas Weidner added a comment - Assigned to Bill
Hide
julien PAULI added a comment -

done in r8099

Show
julien PAULI added a comment - done in r8099
Hide
Thomas Weidner added a comment -

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

Show
Thomas Weidner added a comment - 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

People

Vote (1)
Watch (3)

Dates

  • Created:
    Updated:
    Resolved:

Time Tracking

Estimated:
1d
Original Estimate - 1 day
Remaining:
1d
Remaining Estimate - 1 day
Logged:
Not Specified
Time Spent - Not Specified