55 */
66namespace Magento \CustomerImportExport \Model \Export ;
77
8+ use Magento \Customer \Model \ResourceModel \Address \Collection ;
9+ use Magento \Customer \Model \ResourceModel \Address \CollectionFactory ;
10+ use Magento \Eav \Model \Config ;
11+ use Magento \Framework \App \Config \ScopeConfigInterface ;
12+ use Magento \Framework \DB \Select ;
13+ use Magento \Framework \Stdlib \DateTime \TimezoneInterface ;
14+ use Magento \ImportExport \Model \Export \Entity \AbstractEav ;
15+ use Magento \ImportExport \Model \Export \Factory ;
16+ use Magento \ImportExport \Model \ResourceModel \CollectionByPagesIteratorFactory ;
17+ use Magento \Store \Model \StoreManagerInterface ;
18+
819/**
920 * Customer address export
1021 *
1324 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1425 * @since 100.0.2
1526 */
16- class Address extends \ Magento \ ImportExport \ Model \ Export \ Entity \ AbstractEav
27+ class Address extends AbstractEav
1728{
1829 /**#@+
1930 * Permanent column names
@@ -93,7 +104,7 @@ class Address extends \Magento\ImportExport\Model\Export\Entity\AbstractEav
93104 /**
94105 * Customer addresses collection
95106 *
96- * @var \Magento\Customer\Model\ResourceModel\Address\ Collection
107+ * @var Collection
97108 */
98109 protected $ _addressCollection ;
99110
@@ -118,31 +129,31 @@ class Address extends \Magento\ImportExport\Model\Export\Entity\AbstractEav
118129 *
119130 * @var array
120131 */
121- protected $ _customers = [] ;
132+ protected $ _customers ;
122133
123134 /**
124- * @param \Magento\Framework\App\Config\ ScopeConfigInterface $scopeConfig
125- * @param \Magento\Store\Model\ StoreManagerInterface $storeManager
126- * @param \Magento\ImportExport\Model\Export\ Factory $collectionFactory
127- * @param \Magento\ImportExport\Model\ResourceModel\ CollectionByPagesIteratorFactory $resourceColFactory
128- * @param \Magento\Framework\Stdlib\DateTime\ TimezoneInterface $localeDate
129- * @param \Magento\Eav\Model\ Config $eavConfig
135+ * @param ScopeConfigInterface $scopeConfig
136+ * @param StoreManagerInterface $storeManager
137+ * @param Factory $collectionFactory
138+ * @param CollectionByPagesIteratorFactory $resourceColFactory
139+ * @param TimezoneInterface $localeDate
140+ * @param Config $eavConfig
130141 * @param \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customerColFactory
131- * @param \Magento\CustomerImportExport\Model\Export\ CustomerFactory $eavCustomerFactory
132- * @param \Magento\Customer\Model\ResourceModel\Address\ CollectionFactory $addressColFactory
142+ * @param CustomerFactory $eavCustomerFactory
143+ * @param CollectionFactory $addressColFactory
133144 * @param array $data
134145 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
135146 */
136147 public function __construct (
137- \ Magento \ Framework \ App \ Config \ ScopeConfigInterface $ scopeConfig ,
138- \ Magento \ Store \ Model \ StoreManagerInterface $ storeManager ,
139- \ Magento \ ImportExport \ Model \ Export \ Factory $ collectionFactory ,
140- \ Magento \ ImportExport \ Model \ ResourceModel \ CollectionByPagesIteratorFactory $ resourceColFactory ,
141- \ Magento \ Framework \ Stdlib \ DateTime \ TimezoneInterface $ localeDate ,
142- \ Magento \ Eav \ Model \ Config $ eavConfig ,
148+ ScopeConfigInterface $ scopeConfig ,
149+ StoreManagerInterface $ storeManager ,
150+ Factory $ collectionFactory ,
151+ CollectionByPagesIteratorFactory $ resourceColFactory ,
152+ TimezoneInterface $ localeDate ,
153+ Config $ eavConfig ,
143154 \Magento \Customer \Model \ResourceModel \Customer \CollectionFactory $ customerColFactory ,
144- \ Magento \ CustomerImportExport \ Model \ Export \ CustomerFactory $ eavCustomerFactory ,
145- \ Magento \ Customer \ Model \ ResourceModel \ Address \ CollectionFactory $ addressColFactory ,
155+ CustomerFactory $ eavCustomerFactory ,
156+ CollectionFactory $ addressColFactory ,
146157 array $ data = []
147158 ) {
148159 parent ::__construct (
@@ -178,19 +189,20 @@ public function __construct(
178189 */
179190 protected function _initCustomers ()
180191 {
181- if (empty ($ this ->_customers )) {
192+ if ($ this ->_customers === null ) {
193+ $ this ->_customers = [];
182194 // add customer default addresses column name to customer attribute mapping array
183195 $ this ->_customerCollection ->addAttributeToSelect (self ::$ _defaultAddressAttributeMapping );
184196 // filter customer collection
185197 $ this ->_customerCollection = $ this ->_customerEntity ->filterEntityCollection ($ this ->_customerCollection );
186198
187- $ customers = [];
188- $ addCustomer = function (\Magento \Customer \Model \Customer $ customer ) use (&$ customers ) {
189- $ customers [$ customer ->getId ()] = $ customer ->getData ();
190- };
199+ $ selectIds = $ this ->_customerCollection ->getAllIdsSql ();
200+ $ this ->_customerCollection ->setPageSize ($ this ->_pageSize );
201+ $ pageCount = $ this ->_customerCollection ->getLastPageNumber ();
191202
192- $ this ->_byPagesIterator ->iterate ($ this ->_customerCollection , $ this ->_pageSize , [$ addCustomer ]);
193- $ this ->_customers = $ customers ;
203+ for ($ pageNum = 1 ; $ pageNum <= $ pageCount ; $ pageNum ++) {
204+ $ this ->_customers += $ this ->loadCustomerData ($ selectIds , $ pageNum );
205+ }
194206 }
195207
196208 return $ this ;
@@ -211,7 +223,7 @@ protected function _getHeaderColumns()
211223 /**
212224 * Get customers collection
213225 *
214- * @return \Magento\Customer\Model\ResourceModel\Address\ Collection
226+ * @return Collection
215227 */
216228 protected function _getEntityCollection ()
217229 {
@@ -227,7 +239,7 @@ public function export()
227239 {
228240 // skip and filter by customer address attributes
229241 $ this ->_prepareEntityCollection ($ this ->_getEntityCollection ());
230- $ this ->_getEntityCollection ()->setCustomerFilter (array_keys ($ this ->_customers ));
242+ $ this ->_getEntityCollection ()->setCustomerFilter (array_keys ($ this ->getCustomers () ));
231243
232244 // prepare headers
233245 $ this ->getWriter ()->setHeaderCols ($ this ->_getHeaderColumns ());
@@ -248,7 +260,7 @@ public function exportItem($item)
248260 $ row = $ this ->_addAttributeValuesToRow ($ item );
249261
250262 /** @var $customer \Magento\Customer\Model\Customer */
251- $ customer = $ this ->_customers [$ item ->getParentId ()];
263+ $ customer = $ this ->getCustomers () [$ item ->getParentId ()];
252264
253265 // Fill row with default address attributes values
254266 foreach (self ::$ _defaultAddressAttributeMapping as $ columnName => $ attributeCode ) {
@@ -274,10 +286,8 @@ public function exportItem($item)
274286 */
275287 public function setParameters (array $ parameters )
276288 {
277- // push filters from post into export customer model
289+ // push filters from post into export customer model
278290 $ this ->_customerEntity ->setParameters ($ parameters );
279- $ this ->_initCustomers ();
280-
281291 return parent ::setParameters ($ parameters );
282292 }
283293
@@ -290,4 +300,39 @@ public function getEntityTypeCode()
290300 {
291301 return $ this ->getAttributeCollection ()->getEntityTypeCode ();
292302 }
303+
304+ /**
305+ * Get Customers Data
306+ *
307+ * @return array
308+ */
309+ private function getCustomers (): array
310+ {
311+ $ this ->_initCustomers ();
312+ return $ this ->_customers ;
313+ }
314+
315+ /**
316+ * Load Customers Data
317+ *
318+ * @param Select $selectIds
319+ * @param int $pageNum
320+ * @return array
321+ */
322+ private function loadCustomerData (Select $ selectIds , int $ pageNum = 0 ): array
323+ {
324+ $ select = $ this ->_customerCollection ->getConnection ()->select ();
325+ $ select ->from (
326+ ['customer ' => $ this ->_customerCollection ->getTable ('customer_entity ' )],
327+ ['entity_id ' , 'email ' , 'store_id ' , 'website_id ' , 'default_billing ' , 'default_shipping ' ]
328+ )->where (
329+ 'customer.entity_id IN (?) ' , $ selectIds
330+ );
331+
332+ if ($ pageNum > 0 ) {
333+ $ select ->limitPage ($ pageNum , $ this ->_pageSize );
334+ }
335+
336+ return $ this ->_customerCollection ->getConnection ()->fetchAssoc ($ select );
337+ }
293338}
0 commit comments