Magento2 - How do I write below query function into non-deprecated? without using getCollection()
-
15-04-2021 - |
質問
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;
}
解決
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();
}
}
所属していません magento.stackexchange