unclecheese / silverstripe-gridfield-betterbuttons

Adds new form actions and buttons to the GridField detail form
GNU General Public License v2.0
80 stars 87 forks source link

PHP Fatal error: Allowed memory size of X bytes exhausted - getPreviousRecordID and getNextRecordID #101

Open botzkobg opened 9 years ago

botzkobg commented 9 years ago

Hi,

I have a table with millions of records and start to receive 'PHP Fatal error: Allowed memory size of X bytes exhausted'. After some digging I found the problem in getPreviousRecordID and getNextRecordID (GridFieldBetterButtonsItemRequest). I saw the comment 'WARNING: This does not respect the mutated state of the list (e.g. sorting or filtering).' and think if it's so why you need all the records to get the next/previous ID. You can use something like this:

public function getPreviousRecordID() {
        if (empty($this->owner->record))
            return false;

        $strClass = get_class($this->owner->record);
        $next = DataObject::get($strClass, 'ID < ' . intval($this->owner->record->ID),'ID DESC')->limit(1)->column('ID');
        return isset($next[0])
            ? $next[0]
            : false;
}

public function getNextRecordID() {
    if (empty($this->owner->record))
        return false;

    $strClass = get_class($this->owner->record);
    $next = DataObject::get($strClass, 'ID > ' . intval($this->owner->record->ID),'ID ASC')->limit(1)->column('ID');
    return isset($next[0])
        ? $next[0]
        : false;
}

It will be faster and will not require so much memory.

unclecheese commented 8 years ago

Thanks for this. I don't know that it's safe to assume the sort will be by ID. The sort is whatever it is in the DataList that was originally passed to the GridField constructor. By not respecting the mutated state, I'm referring to filtering/sorting that happened from within the GridField UI.