Issues

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

Issue Type: Improvement Created: 2007-09-05T12:17:09.000+0000 Last Updated: 2008-02-26T12:56:04.000+0000 Status: Resolved Fix version(s): - 1.0.4 (26/Feb/08)

Reporter: julien PAULI (doctorrock83) Assignee: julien PAULI (doctorrock83) Tags: - Zend_Db_Table

Related issues: Attachments:

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

<pre class="highlight">
<?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

<pre class="highlight">
<?php
require_once 'Zend/Db/Table/Exception.php';

class Zend_Db_Table_Rowset_Exception extends Zend_Db_Table_Exception
{
}

Seek use-case :

<pre class="highlight">
<?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 :

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

Comments

Posted by Thomas Weidner (thomas) on 2007-09-08T12:01:41.000+0000

Assigned to Bill

Posted by julien PAULI (doctorrock83) on 2008-02-17T13:40:04.000+0000

done in r8099

Posted by Thomas Weidner (thomas) on 2008-02-17T15:15:05.000+0000

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

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts