src/Forms/SortableUploadField.php
protected function sortManyManyThroughRelation(
        ManyManyThroughList $relation,
        array $idList,
        array $rawList,
        $sortColumn
    ) {
        $relation->getForeignID();
        $dataQuery = $relation->dataQuery();
        $manipulators = $dataQuery->getDataQueryManipulators();
        $manyManyManipulator = null;
        foreach ($manipulators as $manipulator) {
            if ($manipulator instanceof ManyManyThroughQueryManipulator) {
                $manyManyManipulator = $manipulator;
                break;
            }
        }

        if (!$manyManyManipulator) {
            throw new \LogicException('No ManyManyThroughQueryManipulator found');
        }

        $joinClass = $manyManyManipulator->getJoinClass();
        $ownerIDField = $manyManyManipulator->getForeignKey();
        $fileIdField = $manyManyManipulator->getLocalKey();

        $sort = 0;
        foreach ($rawList as $id) {
            if (in_array($id, $idList)) {
                $fileRecord = DataList::create($joinClass)->filter([
                    $ownerIDField => $relation->getForeignID(),
                    $fileIdField  => $id
                ])->first();

                if ($fileRecord) {
                    $fileRecord->setField($sortColumn, $sort++);
                    $fileRecord->write();
                }
            }
        }
    }
protected function sortManyManyThroughRelation(
        ManyManyThroughList $relation,
        array $idList,
        array $rawList,
        $sortColumn
    ) {
        $relation->getForeignID();
        $dataQuery = $relation->dataQuery();
        $manipulators = $dataQuery->getDataQueryManipulators();
        $manyManyManipulator = null;
        foreach ($manipulators as $manipulator) {
            if ($manipulator instanceof ManyManyThroughQueryManipulator) {
                $manyManyManipulator = $manipulator;
                break;
            }
        }

        if (!$manyManyManipulator) {
            throw new \LogicException('No ManyManyThroughQueryManipulator found');
        }

        $joinClass = $manyManyManipulator->getJoinClass();
        $ownerIDField = $manyManyManipulator->getForeignKey();
        $fileIdField = $manyManyManipulator->getLocalKey();

        $sort = 0;
        foreach ($rawList as $id) {
            if (in_array($id, $idList)) {
                $fileRecord = DataList::create($joinClass)->filter([
                    $ownerIDField => $relation->getForeignID(),
                    $fileIdField  => $id
                ])->first();

                if ($fileRecord) {
                    $fileRecord->setField($sortColumn, $sort++);
                    $fileRecord->write();
                }
            }
        }
    }
protected function sortManyManyThroughRelation ( ManyManyThroughList $ relation , array $ idList , array $ rawList , $ sortColumn ) { $ relation -> getForeignID ( ) ; $ dataQuery = $ relation -> dataQuery ( ) ; $ manipulators = $ dataQuery -> getDataQueryManipulators ( ) ; $ manyManyManipulator = null ; foreach ( $ manipulators as $ manipulator ) { if ( $ manipulator instanceof ManyManyThroughQueryManipulator ) { $ manyManyManipulator = $ manipulator ; break ; } } if ( ! $ manyManyManipulator ) { throw new \ LogicException ( 'No ManyManyThroughQueryManipulator found' ) ; } $ joinClass = $ manyManyManipulator -> getJoinClass ( ) ; $ ownerIDField = $ manyManyManipulator -> getForeignKey ( ) ; $ fileIdField = $ manyManyManipulator -> getLocalKey ( ) ; $ sort = 0 ; foreach ( $ rawList as $ id ) { if ( in_array ( $ id , $ idList ) ) { $ fileRecord = DataList :: create ( $ joinClass ) -> filter ( [ $ ownerIDField => $ relation -> getForeignID ( ) , $ fileIdField => $ id ] ) -> first ( ) ; if ( $ fileRecord ) { $ fileRecord -> setField ( $ sortColumn , $ sort ++ ) ; $ fileRecord -> write ( ) ; } } } }
Apply sorting to a many_many_through relation
@param ManyManyThroughList $relation
@param array $idList
@param array $rawList
@param $sortColumn
@throws \SilverStripe\ORM\ValidationException
Apply sorting to a many_many_through relation