src/Common/Model/Base.php
protected function saveAssociatedItems(
        $iItemId,
        array $aAssociatedItems,
        $sAssociatedItemIdColumn,
        $sAssociatedModel,
        $sAssociatedModelProvider
    ) {
        $oAssociatedItemModel = Factory::model($sAssociatedModel, $sAssociatedModelProvider);
        $aTouchedIds          = [];
        $aExistingItemIds     = [];

        //  Get IDs of current items, we'll compare these later to see which ones to delete.
        $aData = [
            'where' => [
                [$oAssociatedItemModel->getTableAlias() . '.' . $sAssociatedItemIdColumn, $iItemId],
            ],
        ];

        $aExistingItems = $oAssociatedItemModel->getAll(null, null, $aData);
        foreach ($aExistingItems as $oExistingItem) {
            $aExistingItemIds[] = $oExistingItem->id;
        }

        // --------------------------------------------------------------------------

        //  Update/insert all known items
        foreach ($aAssociatedItems as $aAssociatedItem) {

            $aAssociatedItem = (array) $aAssociatedItem;

            if (!empty($aAssociatedItem['id'])) {

                //  Safety, no updating of IDs
                $iAssociatedItemId = $aAssociatedItem['id'];
                unset($aAssociatedItem['id']);

                //  Perform update
                if (!$oAssociatedItemModel->update($iAssociatedItemId, $aAssociatedItem)) {
                    throw new ModelException(
                        'Failed to update associated item (' . $sAssociatedModelProvider . ':' . $sAssociatedModel . ') ' . $oAssociatedItemModel->lastError()
                    );
                } else {
                    $aTouchedIds[] = $iAssociatedItemId;
                }

            } else {

                //  Safety, no setting of IDs
                unset($aAssociatedItem['id']);

                //  Ensure the related column is set
                $aAssociatedItem[$sAssociatedItemIdColumn] = $iItemId;

                //  Perform the create
                $iAssociatedItemId = $oAssociatedItemModel->create($aAssociatedItem);
                if (!$iAssociatedItemId) {
                    throw new ModelException(
                        'Failed to create associated item (' . $sAssociatedModelProvider . ':' . $sAssociatedModel . ') ' . $oAssociatedItemModel->lastError()
                    );
                } else {
                    $aTouchedIds[] = $iAssociatedItemId;
                }
            }
        }

        // --------------------------------------------------------------------------

        //  We want to delete items which are no longer in use
        $aIdDiff = array_diff($aExistingItemIds, $aTouchedIds);

        //  Delete those we no longer require
        if (!empty($aIdDiff)) {
            if (!$oAssociatedItemModel->deleteMany($aIdDiff)) {
                throw new ModelException(
                    'Failed to delete old associated items (' . $sAssociatedModelProvider . ':' . $sAssociatedModel . ') ' . $oAssociatedItemModel->lastError()
                );
            }
        }

        return true;
    }
protected function saveAssociatedItems(
        $iItemId,
        array $aAssociatedItems,
        $sAssociatedItemIdColumn,
        $sAssociatedModel,
        $sAssociatedModelProvider
    ) {
        $oAssociatedItemModel = Factory::model($sAssociatedModel, $sAssociatedModelProvider);
        $aTouchedIds          = [];
        $aExistingItemIds     = [];

        //  Get IDs of current items, we'll compare these later to see which ones to delete.
        $aData = [
            'where' => [
                [$oAssociatedItemModel->getTableAlias() . '.' . $sAssociatedItemIdColumn, $iItemId],
            ],
        ];

        $aExistingItems = $oAssociatedItemModel->getAll(null, null, $aData);
        foreach ($aExistingItems as $oExistingItem) {
            $aExistingItemIds[] = $oExistingItem->id;
        }

        // --------------------------------------------------------------------------

        //  Update/insert all known items
        foreach ($aAssociatedItems as $aAssociatedItem) {

            $aAssociatedItem = (array) $aAssociatedItem;

            if (!empty($aAssociatedItem['id'])) {

                //  Safety, no updating of IDs
                $iAssociatedItemId = $aAssociatedItem['id'];
                unset($aAssociatedItem['id']);

                //  Perform update
                if (!$oAssociatedItemModel->update($iAssociatedItemId, $aAssociatedItem)) {
                    throw new ModelException(
                        'Failed to update associated item (' . $sAssociatedModelProvider . ':' . $sAssociatedModel . ') ' . $oAssociatedItemModel->lastError()
                    );
                } else {
                    $aTouchedIds[] = $iAssociatedItemId;
                }

            } else {

                //  Safety, no setting of IDs
                unset($aAssociatedItem['id']);

                //  Ensure the related column is set
                $aAssociatedItem[$sAssociatedItemIdColumn] = $iItemId;

                //  Perform the create
                $iAssociatedItemId = $oAssociatedItemModel->create($aAssociatedItem);
                if (!$iAssociatedItemId) {
                    throw new ModelException(
                        'Failed to create associated item (' . $sAssociatedModelProvider . ':' . $sAssociatedModel . ') ' . $oAssociatedItemModel->lastError()
                    );
                } else {
                    $aTouchedIds[] = $iAssociatedItemId;
                }
            }
        }

        // --------------------------------------------------------------------------

        //  We want to delete items which are no longer in use
        $aIdDiff = array_diff($aExistingItemIds, $aTouchedIds);

        //  Delete those we no longer require
        if (!empty($aIdDiff)) {
            if (!$oAssociatedItemModel->deleteMany($aIdDiff)) {
                throw new ModelException(
                    'Failed to delete old associated items (' . $sAssociatedModelProvider . ':' . $sAssociatedModel . ') ' . $oAssociatedItemModel->lastError()
                );
            }
        }

        return true;
    }
protected function saveAssociatedItems ( $ iItemId , array $ aAssociatedItems , $ sAssociatedItemIdColumn , $ sAssociatedModel , $ sAssociatedModelProvider ) { $ oAssociatedItemModel = Factory :: model ( $ sAssociatedModel , $ sAssociatedModelProvider ) ; $ aTouchedIds = [ ] ; $ aExistingItemIds = [ ] ; //  Get IDs of current items, we'll compare these later to see which ones to delete. $ aData = [ 'where' => [ [ $ oAssociatedItemModel -> getTableAlias ( ) . '.' . $ sAssociatedItemIdColumn , $ iItemId ] , ] , ] ; $ aExistingItems = $ oAssociatedItemModel -> getAll ( null , null , $ aData ) ; foreach ( $ aExistingItems as $ oExistingItem ) { $ aExistingItemIds [ ] = $ oExistingItem -> id ; } // -------------------------------------------------------------------------- //  Update/insert all known items foreach ( $ aAssociatedItems as $ aAssociatedItem ) { $ aAssociatedItem = ( array ) $ aAssociatedItem ; if ( ! empty ( $ aAssociatedItem [ 'id' ] ) ) { //  Safety, no updating of IDs $ iAssociatedItemId = $ aAssociatedItem [ 'id' ] ; unset ( $ aAssociatedItem [ 'id' ] ) ; //  Perform update if ( ! $ oAssociatedItemModel -> update ( $ iAssociatedItemId , $ aAssociatedItem ) ) { throw new ModelException ( 'Failed to update associated item (' . $ sAssociatedModelProvider . ':' . $ sAssociatedModel . ') ' . $ oAssociatedItemModel -> lastError ( ) ) ; } else { $ aTouchedIds [ ] = $ iAssociatedItemId ; } } else { //  Safety, no setting of IDs unset ( $ aAssociatedItem [ 'id' ] ) ; //  Ensure the related column is set $ aAssociatedItem [ $ sAssociatedItemIdColumn ] = $ iItemId ; //  Perform the create $ iAssociatedItemId = $ oAssociatedItemModel -> create ( $ aAssociatedItem ) ; if ( ! $ iAssociatedItemId ) { throw new ModelException ( 'Failed to create associated item (' . $ sAssociatedModelProvider . ':' . $ sAssociatedModel . ') ' . $ oAssociatedItemModel -> lastError ( ) ) ; } else { $ aTouchedIds [ ] = $ iAssociatedItemId ; } } } // -------------------------------------------------------------------------- //  We want to delete items which are no longer in use $ aIdDiff = array_diff ( $ aExistingItemIds , $ aTouchedIds ) ; //  Delete those we no longer require if ( ! empty ( $ aIdDiff ) ) { if ( ! $ oAssociatedItemModel -> deleteMany ( $ aIdDiff ) ) { throw new ModelException ( 'Failed to delete old associated items (' . $ sAssociatedModelProvider . ':' . $ sAssociatedModel . ') ' . $ oAssociatedItemModel -> lastError ( ) ) ; } } return true ; }
Save associated items for an object

@param int    $iItemId                  The ID of the main item
@param array  $aAssociatedItems         The data to save, multi-dimensional array of data
@param string $sAssociatedItemIdColumn  The name of the ID column in the associated table
@param string $sAssociatedModel         The name of the model which is responsible for associated items
@param string $sAssociatedModelProvider What module provide the associated item model

@return bool
@throws ModelException
Save associated items for an object