Reset function and DAOResultFactory

OJS development discussion, enhancement requests, third-party patches and plug-ins.

Moderators: jmacgreg, btbell, michael, bdgregg, barbarah, asmecher

Forum rules
The Public Knowledge Project Support Forum is moving to http://forum.pkp.sfu.ca

This forum will be maintained permanently as an archived historical resource, but all new questions should be added to the new forum. Questions will no longer be monitored on this old forum after March 30, 2015.
mark
Posts: 31
Joined: Fri Jul 11, 2008 10:57 am

Reset function and DAOResultFactory

Postby mark » Tue Oct 14, 2008 12:39 pm

What I would like to do is to have use a DAOResultsFactory twice. The first iteration through would be to update the DAO objects in the list, and the second pass would be to display the obects. However, DAOResultsFactory are one-way iterators with no ability to go back to the beginning or to reset the iterator to the beginning. Currently, once a user has iterated through the list of Objects, the results factory is essentially dead weight. This essentially defeats the modifications introduced in the first pass.

I would like to add a function, reset() but am hesitant of modifying a parent class because there is a grandparent (i.e., core.Iterator) and so it may require a longer sojourn into the realm of the most abstract classes. Unless there already is a way of reseting a DAOResultsFactory, my plan though is to add a null reset function to iterator

Code: Select all

core.Iterator.inc.php

function reset() { return;}


And then overload reset() in the DAOResultFactory, using code derived from the DAOResultFactory constructor.

Code: Select all

db.DAOResultFactory.inc.php

function reset(){
              if (!$this->records || $this->records->EOF) {
                        if ($records) $records->Close();
                        $this->records = null;
                        $this->wasEmpty = true;
                        $this->page = 1;
                        $this->isFirst = true;
                        $this->isLast = true;
                        $this->count = 0;
                        $this->pageCount = 1;
                } else {
                       $this->wasEmpty = false;
                        $this->page = $this->records->AbsolutePage();
                        $this->isFirst = $this->records->atFirstPage();
                        $this->isLast = $this->records->atLastPage();
                        $this->count = $this->records->MaxRecordCount();
                        $this->pageCount = $this->records->LastPageNo()
                 }
}


Can anyone advise on this? Perhaps there is a much easier way of doing this with the existing code base?

asmecher
Posts: 10015
Joined: Wed Aug 10, 2005 12:56 pm
Contact:

Re: Reset function and DAOResultFactory

Postby asmecher » Tue Oct 14, 2008 12:49 pm

Hi Mark,

The best way to do this is to convert the iterator to an array, then loop through it twice using foreach() or similar:

Code: Select all

$iterator =& $someDao->someGetterFunction();
$array =& $iterator->toArray();

foreach ($array as $item) {
    // Do something with $item
}

foreach ($array as $item) {
    // Do something else with $item
}
Don't forget that foreach is a by-value operation, so if you're modifying the objects in the first pass, it's probably better to use something like:

Code: Select all

$iterator =& $someDao->someGetterFunction();
$array =& $iterator->toArray();

foreach ($array as $key => $junk) {
    $item =& $array[$key];
    // Do something with $item
    unset ($item);
}

foreach ($array as $key => $junk) {
    $item =& $array[$key];
    // Do something else with $item
    unset ($item);
}
Regards,
Alec Smecher
Public Knowledge Project Team


Return to “OJS Development”

Who is online

Users browsing this forum: No registered users and 1 guest