0
votes

The combination of Collection function (in my case 'filter') and paginate() causes a error. The simplified PHP code is below:

$rooms = $this->Rooms
    ->find('all')
    ->formatResults(function ($results) {
        return $results->map(function ($row) {
            //... 
            $row->free_booking=0;      
            if($someCondition) $row->free_booking=1;
            //...

            return $row;
        });        
    })
    ->filter(function ($booking, $key) {
        return $booking->free_booking == 1;
    });
$this->set('rooms',$this->paginate($rooms));

The error is below:

Error: Call to undefined method ArrayIterator::alias() File .../public_html/vendor/cakephp/cakephp/src/Controller/Component/PaginatorComponent.php Line: 174


How to solve this problem?

1

1 Answers

2
votes

You are calling filter() on the query object, that will cause the query to be executed, and converted to a collection, and paginating collections isn't supported.

You should generally refrain from manipulating results (adding/removing entries) on PHP level if you're using pagination, this will almost certainly mess up your pagination, as the number of records on PHP level won't match the counted records on SQL level.

Move your filtering logic into the query instead, ie filter on SQL level.