开发者

How php iterator class works ? (Iterate through zend table row set)

开发者 https://www.devze.com 2023-03-20 06:36 出处:网络
I have object which contains 100 records. I want to iterate through it and delete all the data in the object.

I have object which contains 100 records. I want to iterate through it and delete all the data in the object.

How can I do this in PHP iterator class ?

(Object is ZEND table row set object)

(Here delete means we are just making delete_flag in the database to 1. Data won't be deleted physically from the database.)

Eg:

$zendTableRowSetObject->list[0]->delete_flag = 1
$zendTableRowSetObject->list[2]->delete_flag = 1
$zendTableRowSetObject->list[3]->delete_flag = 1

$zendTableRowSetObject->save(); 

->save() is the Zend function, this will update the object which used to calls this metho开发者_StackOverflow中文版d.

(Other than this any other method http://php.net/manual/en/language.oop5.iterations.php)

(Without using foreach loop is there any way to do it ?)

Give me some examples .

Here is my iterator class

class PersonListIter implements Iterator
{
     protected $_PersonList;

    /**
     * Index of current entries
     * It's used for iterator
     * @var integer
     */
    protected $_entryIndex = 0;

    /**
     * Entries data sets
     * @var array
     */
    protected $_entries;

    /*
     * Initialization of data. 
     * 
     * @params  Zend_Db_Table_Rowset    $list   Row Object
     * @return  null
     */    
    public function __construct ( $list )
    {
        $this->_PersonList = $list;
        $this->_entryIndex = 0;
        $this->_entries = $list->getCount();
    }

    /*
     * Iterator interface method to rewind index
     * @return  null
     */     
    public function rewind()
    {
        $this->_entryIndex = 0;
    }

    /*
     * Iterator interface method to return Current entry
     * @return  Zend_Db_Table_Row   Current Entry
     */         
    public function current()
    {
        return $this->_PersonList->getElement($this->_entryIndex);
    }

    /*
     * Iterator interface method to return index of current entry
     * @return  int     Current Entry Index
     */     
    public function key()
    {
        return $this->_entryIndex;
    }

    /*
     * Iterator interface method to set the next index
     * @return  null
     */      
    public function next()
    {
        $this->_entryIndex += 1;
    }

    /*
     * Iterator interface method to validate the current index
     * @return  enum    0/1 
     */      
    public function valid()
    {
        return (0 <= $this->_entryIndex && $this->_entryIndex < $this->entries)?1:0;
    }

} // class PersonListIter

$zendTableRowSetObject is the PersonList object in the iterator class


You cannot delete all of them at once, you have to iterate through (using foreach or while in conjunction with next()) to delete them.

While surfing I have found the following link which might interest you. This explains the use of implement iterator pattern in PHP in an nice way. >> http://www.fluffycat.com/PHP-Design-Patterns/Iterator/

0

精彩评论

暂无评论...
验证码 换一张
取 消