使用Collection类获取客户的数据

管理员组 admin 2018-4-17 467

customer 集合类 在 Magento\Customer\Model\ResourceModel\Customer\Collection.php
order集合类 在 Magento\Sales\Model\ResourceModel\Order\Collection.php
product集合类 在Magento\Catalog\Model\ResourceModel\Product\Collection.php

获取数据

<?php namespace Veriteworks\ExportData\Controller\Index; class Collection extends \Magento\Framework\App\Action\Action{ public function execute(){ $customerCollection = $this->_objectManager->create('Magento\Customer\Model\ResourceModel\Customer\Collection'); } }

过滤要采集的数据

使用AttributeToFillter()按元素值过滤。
只检索邮件地址为“[email&#160;protected]”的客户

 $customerCollection = $this->_objectManager->create('Magento\Customer\Model\ResourceModel\Customer\Collection')->addAttributeToFilter('email', '[email&#160;protected]');

仅检索产品名称包括“pen”的产品

$productCollection = $this->_objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection')->addAttributeToFilter('name', array('like' => '%pen%'));

订单排序

要按升序降序重新排列数据,使用setOrder()
按Entitiy_id以降序重新排列

 $customerCollection = $this->_objectManager->create('Magento\Customer\Model\ResourceModel\Customer\Collection')->setOrder('entity_id', 'DESC');

按updated_at以降序重新排列

 $customerCollection = $this->_objectManager->create('Magento\Customer\Model\ResourceModel\Customer\Collection')->setOrder('updated_at', 'ASC');

检索元素

为了得到我们使用每个getter方法的元素
firstName

<?php namespace Veriteworks\ExportData\Controller\Index; class Collection extends \Magento\Framework\App\Action\Action{ public function execute(){ $customerCollection = $this->_objectManager->create('Magento\Customer\Model\ResourceModel\Customer\Collection'); foreach ($customerCollection as $customer) { echo $customer->getFirstname() ."<br />"; } } }
最新回复 (0)
返回
发帖