Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
five_repository_customers.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
10 use Magento\Customer\Api\Data\CustomerInterfaceFactory;
12 use Magento\Eav\Model\Config as EavModelConfig;
16 
17 $objectManager = Bootstrap::getObjectManager();
18 
20 $customerRepository = $objectManager->create(CustomerRepositoryInterface::class);
22 $customerFactory = $objectManager->get(CustomerInterfaceFactory::class);
23 
24 for ($i = 1; $i <= 5; $i++) {
26  $customer = $customerFactory->create();
27  $customer->setFirstname('John')
28  ->setGroupId(1)
29  ->setLastname('Smith')
30  ->setWebsiteId(1)
31  ->setEmail('customer'.$i.'@example.com');
32  try {
33  $customerRepository->save($customer, 'password');
34  } catch (\Exception $e) {
35  }
36 }
37 
39 $eavConfig = $objectManager->get(EavModelConfig::class);
40 $eavConfig->clear();
41 
43 $indexerRegistry = $objectManager->create(IndexerRegistry::class);
45 $indexer = $indexerRegistry->get(Customer::CUSTOMER_GRID_INDEXER_ID);
46 try {
47  $indexer->reindexAll();
48 } catch (\Exception $e) {
49 }
$customer
Definition: customers.php:11
$i
Definition: gallery.phtml:31