0

I created a custom database table and now want to fetch data, but getCollection method is deprecated and I can't find any other method that suits my case. Here's the example of my code:

use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory;
use <MyName>\<MyModule>\Model\UserUpdateFactory;

class CustomerChangeTracker {

private $customer;

private $previousCustomer;

private $current = [];


public function __construct(CollectionFactory $customer, UserUpdateFactory $previousCustomer)
{
    $this->customer = $customer;
    $this->previousCustomer = $previousCustomer;
}

public function getChangesSinceLastTime(): array
{
    $current = [];
    $collection = $this->customer->create();
    foreach ($collection as $customer) {
        $current[$customer->getId()] = $customer->getData('updated_at');
    }
    $this->setCurrent($current);

    $previous = [];
    foreach ($this->previousCustomer->create()->getCollection() as $customer) {
        $previous[$customer->getData('customer_id')] = $customer->getData('updated_at');
    }
  • you shouldnt need to add the getCollection as $this->previousCustomer->create() is doing that already – Dava Gordon Aug 20 at 9:38
  • did you create resourcemodel and model for your custom table? – aravind Aug 20 at 10:50
  • Yes, I did create Resource Model and Model. – Violeta Aug 20 at 11:14
  • Thank you all, I realized I was using model factory, not collection factory. Now everything is ok. – Violeta Aug 20 at 13:10
0

You don't need to call getCollection on a collection factory class. Because as the name says it is already a collection. Alternatively you could use the resource model and call the collection from it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.