Magento 2 – Call to Undefined Method on Custom Collection Resource Model

collection;magento2module

I have been trying to figure this out for 2 days.
I created a custom collection for my module structured as follows

app/code/Morello/Customproducts/Model/CustomProducts.php

namespace Morello\Customproducts\Model;

use Magento\Framework\Model\AbstractModel;

class CustomProducts extends AbstractModel
{
    protected function _construct()
    {
        $this->_init('Morello\Customproducts\Model\ResourceModel\CustomProducts');
    }
}

app/code/Morello/Customproducts/Model/ResourceModel/CustomProducts.php

namespace Morello\Customproducts\Model\ResourceModel;

use Magento\Framework\Model\ResourceModel\Db\AbstractDb;

class CustomProducts extends AbstractDb
{
    protected function _construct()
    {
        $this->_init('customer_products', 'customproduct_id');
    }
}

app/code/Morello/Customproducts/Model/ResourceModel/CustomProducts/Collection.php

namespace Morello\Customproducts\Model\ResourceModel\CustomProducts;

use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;

class Collection extends AbstractCollection
{

    protected function _construct()
    {
        $this->_init('Morello\Customproducts\Model\CustomProducts', 'Morello\Customproducts\Model\ResourceModel\CustomProducts');
    }

}

In my observer I use the CustomProductsFactory and call methods on it. addFieldToSelect, addFieldToFilter, load, setData and save all work, but when I try to call the delete method to remove an entry in the table it throws a fatal error

Fatal error: Uncaught Error: Call to undefined method Morello\Customproducts\Model\ResourceModel\CustomProducts\Collection::delete() in etc…

What am I missing? The Collection class in the ResourceModel\CustomProducts folder extends the right framework class…

Any help is appreciated.

EDIT:

This is a very simplified version of my observer…

app/code/Morello/Customproducts/Observer/Backend/CustomerSaveAfter.php

namespace Morello\Customproducts\Observer\Backend;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Morello\Customproducts\Model\CustomProductsFactory;

class CustomerSaveAfter implements ObserverInterface
{

    private $customProductsFactory;

    public function __construct(
        CustomProductsFactory $customProductsFactory
    )
    {
        $this->customProductsFactory = $customProductsFactory;
    }

    public function execute(Observer $observer)
    {
        $customerId = '1';
        $productsToRemoveFromCustomer = array(1, 3, 27);

        $customProductModel = $this->customProductsFactory->create();
        $customProductCollection = $customProductModel->getCollection();

        $customProductCollection
            ->addFieldToSelect('customproduct_id')
            ->addFieldToFilter(
                'customer_id',
                ['eq' => $customerId ]
            )->addFieldToFilter(
                'product_id',
                ['in' => $productsToRemoveFromCustomer ]
            )
            ->load();
            ->delete();
    }
}

Best Answer

the collection class does not have a delete method. Only the model and resource model classes do have this.

If you could further describe, what your observer should do, I could maybe provide further information