OJS OCS OMP OHS

You are viewing the PKP Support Forum | PKP Home Wiki



Reset function and DAOResultFactory

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

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

Forum rules
Developer Resources:

Documentation: The OJS Technical Reference and the OJS API Reference are both available from the OJS Documentation page.

Git: You can access our public Git Repository here. Comprehensive Git usage instructions are available on the wiki.

Bugzilla: You can access our Bugzilla report tracker here.

Search: You can use our Google Custom Search to search across our main website, the support forum, and Bugzilla.

Questions and discussion are welcome, but if you have a workflow or usability question you should probably post to the OJS Editorial Support and Discussion subforum; if you have a technical support question, try the OJS Technical Support subforum.

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?
mark
 
Posts: 31
Joined: Fri Jul 11, 2008 10:57 am

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
asmecher
 
Posts: 8869
Joined: Wed Aug 10, 2005 12:56 pm


Return to OJS Development

Who is online

Users browsing this forum: No registered users and 2 guests