XF 2.2 Undefined method 'array_map'

FoxSecrets

Active member
I'm getting error "undefined method array_map" in my function.
Should I import some class in order to work? Or any special xf tag to loop in query results?

PHP
Code:
public function actionExpire()
    {
        $books = $this->finder('FOX\Books:Book')->where('status', 'valid')->fetch();
        $books->array_map(
            function($book) {
                if ($book->expiration < time())
                {
                    $book->status = 'expired';
                    $book->save();
                }
            }
        );


        return $this->redirect($this->buildLink('fox-books'));
    }
 
When you call \XF\Mvc\Entity\Finder::fetch, you get back a \XF\Mvc\Entity\ArrayCollection object which has no array_map method.

If you were trying to call the array_map function instead, you would need to convert the collection into an array and pass it as the second argument.

PHP:
$bookStatuses = array_map(fn (Book $b) => $b->status, $books->toArray());

Collections do have some useful methods, so you could also accomplish the above with:
PHP:
$bookStatuses = $books->pluckNamed('status', 'book_id');

However since you're not really creating a map here, iterating is probably the best approach.
 
Last edited:
Back
Top Bottom