What is the best way to load Customer by custom attribute? I create custom module. What should I overwrite?
- Model/Customer
- Model/ResourceModel/Customer
- Model/ResourceModel/CustomerRepository
What is the best way?
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityWe should try with Service Contracts Layer.
Take a look: vendor/magento/module-backend/Model/Search/Customer::load()
$searchFields = ['firstname', 'lastname', 'company'];
$filters = [];
foreach ($searchFields as $field) {
$filters[] = $this->filterBuilder
->setField($field)
->setConditionType('like')
->setValue($this->getQuery() . '%')
->create();
}
$this->searchCriteriaBuilder->addFilters($filters);
$searchCriteria = $this->searchCriteriaBuilder->create();
$searchResults = $this->customerRepository->getList($searchCriteria);
We can use Magento\Customer\Api\CustomerRepositoryInterface::getList()
to retrieve customers which match a specified criteria.