silverstripe / silverstripe-framework

Silverstripe Framework, the MVC framework that powers Silverstripe CMS
https://www.silverstripe.org
BSD 3-Clause "New" or "Revised" License
720 stars 820 forks source link

FIX Don't throw exception for empty eagerloaded relation #11220

Closed GuySartorelli closed 1 month ago

GuySartorelli commented 1 month ago

Fixes a bug where eagerloading an empty many_many relation would result in an exception. It was caused by adding 'AND ' . $childIDField . ' IN (' . $fetchedIDsAsString . ')' to the SQL query for fetching join rows, without checking that we had actually found any child IDs in the first place.

Can be triggered by either having an empty relation list, or by causing the list to be empty by manipulating the eagerloaded query with the new syntax.

Having this in _config.php is enough to reproduce the original bug:

<?php

use SilverStripe\Security\Group;

$items = Group::get()->eagerLoad('Roles');
foreach ($items as $item) {
    // no-op
}

Issue

GuySartorelli commented 1 month ago

Oops! Sure should. Retargeted and reran CI.