1

How do I write below function into non-deprecated? In Magento2. without using getCollection()

 public function getNewCustomer()
    {
        $customerData = $this->customerFactory->create()->getCollection();
        $customerData->addAttributeToSelect("*");
        $currentDate = date("Y-m-d H:i:s"); // Y-m-d h:i:s
        $newDate = strtotime('-30 MINUTE', strtotime($currentDate));
        $newDate = date('Y-m-d H:i:s', $newDate);
        $customerData->addFieldToFilter('created_at', ['gteq' => $newDate]);
        $customerData->addFieldToFilter('created_at', ['lteq' => $currentDate]);
        $customerData->load();

        return $customerData;
    }

Please help I changed below, but its not work

public function getNewCustomer()
{
    $customerData = $this->customerFactory->create();
    $customerData->getSelect()
         ->where(
            'created_at >= ?',
            $customerData->getConnection()->getDateSubSql(
                new Zend_Db_Expr('NOW()'),
                '30',
                AdapterInterface::INTERVAL_MINUTE
            )
        );

    return $customerData;
}
1
  • which dependency you are using for "$this->customerFactory" ?
    – Pawan
    Jan 6, 2021 at 6:21

1 Answer 1

1

Model class getCollection() is equivalent to Resource Collection Class.

Suppose Model class \Magento\Customer\Model\Customer and its resource Collection Class \Magento\Customer\Model\ResourceModel\Customer\Collection

Instead of \Magento\Customer\Model\Customer->getCollection You can use \Magento\Customer\Model\ResourceModel\Customer\Collection

<?php
namespace {NameSpace};

class OrderTest
{
    /**
     * @var \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory
     */
    private $customerCollectionFactory;

    public function __construct(
        \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customerCollectionFactory
    ) {
        $this->customerCollectionFactory = $customerCollectionFactory;
    }
    public function hello()
    {
        /**
         * @var  \Magento\Customer\Model\ResourceModel\Customer\Collection $customerCollection
         */
        $customerData = $this->customerCollectionFactory->create();
        $customerData->addAttributeToSelect("*");
        $currentDate = date("Y-m-d H:i:s"); // Y-m-d h:i:s
        $newDate = strtotime('-30 MINUTE', strtotime($currentDate));
        $newDate = date('Y-m-d H:i:s', $newDate);
        $customerData->addFieldToFilter('created_at', ['gteq' => $newDate]);
        $customerData->addFieldToFilter('created_at', ['lteq' => $currentDate]);
        $customerData->load();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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