Not a bug Finders not returning false on empty/non-existent result

Snog

Well-known member
Affected version
2.1
Take this example where 687 is a non-existent category ID:

Code:
$mediaItemFinder = $this->finder('XFMG:MediaItem')
            ->where([
            'category_id' => 687
        ]);

$mediaIds = $mediaItemFinder->pluckFrom('media_id')->fetch(1000);

if(!$mediaIds)
{
    echo 'Nope'; die();
}
else
{
    print_r($mediaIds); die();
}

Will always return
XF\Mvc\Entity\ArrayCollection Object ( [entities:protected] => Array ( ) [populated:protected] => 1 )
 
That's correct.

You can either convert the object to an array:
PHP:
if (!$mediaIds->toArray()
Or check if there are any entries:
PHP:
if (!$mediaIds->count())
 
That's correct.

You can either convert the object to an array:
PHP:
if (!$mediaIds->toArray()
Or check if there are any entries:
PHP:
if (!$mediaIds->count())
I could have sworn they use to return FALSE on empty or not found. :(

Might want to change your code in XFMG then ;)

Code:
$mediaIds = $mediaItemFinder->pluckFrom('media_id')->fetch(1000);
if (!$mediaIds)
{
   return $this->complete();
}
 
Top Bottom