具有匹配ID的输出数组

时间:2016-08-20 00:30:27

标签: arrays if-statement foreach magento-1.9 array-merge

我有2个集合(customer和subinfo)从2个单独的数据库表中提取信息。两个表信息都是相关的,我需要加入/合并我已经完成的信息。

但不幸的是,它输出了合并信息的客户总数(5),然后重复输出,共输出10个。见下图。

enter image description here

我需要if语句或其他东西告诉它$collection->getId() == $subcollection->getEntity_id()然后只输出匹配id的那些。

我怎样才能实现这一目标?

更新

 class SM_XPos_Model_Search_Customer extends Varien_Object
{
protected $_isXman = false;
protected $_totalCustomer;


public function __construct()
{
    $this->_totalCustomer = Mage::app()->getCache()->load('totalCustomer');
}

/**
 * Load search results
 *
 * @return Mage_Adminhtml_Model_Search_Customer
 */
public function load()
{
    $arr = array();

    if (!$this->hasStart() || !$this->hasLimit() || !$this->hasQuery()) {
        $this->setResults($arr);
        return $this;
    }

    $isXman = 0;
    if (Mage::getStoreConfig('xmanager/general/enabled') == 1) {
        /*xmanager has been installed*/
        $isXman = 1;
    }
    if ($isXman == 0) {
        $collection = Mage::getResourceModel('customer/customer_collection')
            ->addNameToSelect()
            ->addAttributeToSelect('*')
            ->joinAttribute('telephone', 'customer_address/telephone', 'default_billing', null, 'left')
            ->joinAttribute('city', 'customer_address/city', 'default_billing', null, 'left')
            ->joinAttribute('street', 'customer_address/street', 'default_billing', null, 'left')
            ->addAttributeToFilter(array(
                array('attribute' => 'firstname', 'like' => '%' . $this->getQuery() . '%'),
                array('attribute' => 'lastname', 'like' => '%' . $this->getQuery() . '%'),
                array('attribute' => 'name', 'like' => '%' . $this->getQuery() . '%'),
                array('attribute' => 'telephone', 'like' => $this->getQuery() . '%'),
                array('attribute' => 'email', 'like' => '%' . $this->getQuery() . '%'),
            ))
            ->setPage($this->getStart(), $this->getLimit())
            ->load();

        $subcollection = Mage::getModel('sublogin/sublogin')->getCollection();
    } else {
        $per = Mage::getModel('xmanager/permission');
        if ($per->getShareOrder() == 1 || $per->getPermission() == '0') {
            $collection = Mage::getResourceModel('customer/customer_collection')
                ->addNameToSelect()
                ->addAttributeToSelect('*')
                ->joinAttribute('telephone', 'customer_address/telephone', 'default_billing', null, 'left')
                ->joinAttribute('city', 'customer_address/city', 'default_billing', null, 'left')
                ->joinAttribute('street', 'customer_address/street', 'default_billing', null, 'left')
                ->addAttributeToFilter(array(
                    array('attribute' => 'firstname', 'like' => '%' . $this->getQuery() . '%'),
                    array('attribute' => 'lastname', 'like' => '%' . $this->getQuery() . '%'),
                    array('attribute' => 'name', 'like' => '%' . $this->getQuery() . '%'),
                    array('attribute' => 'telephone', 'like' => $this->getQuery() . '%'),
                    array('attribute' => 'email', 'like' => '%' . $this->getQuery() . '%'),
                ))
                ->setPage($this->getStart(), $this->getLimit())
                ->load();

            $subcollection = Mage::getModel('sublogin/sublogin')->getCollection();
        } else {
            $allow = $per->getAllowAfterAss();
            $ass = $per->getAssigned();
            $isAss = '0';
            foreach ($ass as $as) {
                if ($as != '0') {
                    $isAss = '1';
                }
            }
            if ($allow == '0' && $isAss != '0') {
                $this->setResults($arr);
                return $this;
            }
            $arrAdminId = array();
            $currentId = $per->getCurrentAdmin();
            $currentId = $currentId['id'];

            $arrAdminId[] = $currentId;
            foreach ($per->getIdReceive() as $id) {
                $arrAdminId[] = $id;
            }
            $collection = Mage::getResourceModel('customer/customer_collection')
                ->addNameToSelect()
                ->addAttributeToSelect('admin_id')
                ->addAttributeToSelect('email')
                ->addAttributeToSelect('vehicle_type')
                ->addAttributeToSelect('created_at')
                ->addAttributeToSelect('group_id')
                ->addAttributeToSelect('*')
                ->joinAttribute('telephone', 'customer_address/telephone', 'default_billing', null, 'left')
                ->joinAttribute('city', 'customer_address/city', 'default_billing', null, 'left')
                ->joinAttribute('street', 'customer_address/street', 'default_billing', null, 'left')
                ->addAttributeToFilter(array(
                    array('attribute' => 'firstname', 'like' => '%' . $this->getQuery() . '%'),
                    array('attribute' => 'lastname', 'like' => '%' . $this->getQuery() . '%'),
                    array('attribute' => 'name', 'like' => '%' . $this->getQuery() . '%'),
                    array('attribute' => 'telephone', 'like' => $this->getQuery() . '%'),
                    array('attribute' => 'email', 'like' => '%' . $this->getQuery() . '%'),
                ))
                ->addAttributeToFilter('admin_id', array('in' => $arrAdminId))
                ->setPage($this->getStart(), $this->getLimit())
                ->load();

            $subcollection = Mage::getModel('sublogin/sublogin')->getCollection();
        }
    }


    $storeId = Mage::helper('xpos/product')->getCurrentSessionStoreId();
    $accountShare = Mage::getStoreConfig('customer/account_share/scope');
    foreach ($subcollection as $sublogin) {
    foreach ($collection->getItems() as $customer) {

        $customer_website = $customer->getData('website_id');
        $customer_store = $customer->getData('store_id');
        if ($customer_store == 0 && $customer_website != 0) {
            $website = Mage::app()->getWebsite($customer_website);
            $customer_store = $website->getData('default_group_id');
        }
        if ($accountShare && $customer_website != Mage::app()->getStore($storeId)->getWebsiteId()) continue;
        $customerAddressId = $customer->getDefaultBilling();
        $customerShippingAddressId = $customer->getDefaultShipping();
        $customerBillingAddressId = $customer->getDefaultBilling();
//            if ($customerAddressId){
        $address = Mage::getModel('customer/address')->load($customerAddressId);
        $shippingAddress = Mage::getModel('customer/address')->load($customerShippingAddressId);
        $billingAddress = Mage::getModel('customer/address')->load($customerBillingAddressId);
//            }


        //TODO: [PENDING] If existing billing/shipping address. Will fill to the Billing/shipping form.
        if ($sublogin) {
            $data = array(
                'id'  => $sublogin->getId(),
                'cusid'  => $customer->getid(),
                'subid'  => $sublogin->getEntity_id(),
                'type'        => Mage::helper('adminhtml')->__('Customer'),
                'name'  => $sublogin->getFirstname().' '. $sublogin->getLastname(),
                'fname'   => $sublogin->getFirstname(),
                'lname'  => $sublogin->getLastname(),
                'email'     => $sublogin->getEmail(),
                'telephone' => $sublogin->getMobile_number(),
                'vid'  => $sublogin->getVehicle_id(),
                'vtype'   => $sublogin->getVehicle_type(),
                'vmake'       => $sublogin->getVehicle_make(),
                'vmodel'     => $sublogin->getVehicle_model(),
                'cname'  => $sublogin->getCname(),
            );


            if ($address->getId()) {
                $customerarray = array(
                    'description' => $customer->getCompany(),
                    'group_id'    => $customer->getData('group_id'),
                    'firstname'   => $billingAddress->getData('firstname'),
                    'lastname'    => $billingAddress->getData('lastname'),
                    'city'        => $billingAddress->getData('city'),
                    'street'      => $billingAddress->getData('street'),
                    'country_id'  => $billingAddress->getData('country_id'),
                    'region'      => $billingAddress->getData('region'),
                    'region_id'   => $billingAddress->getData['region_id'],
                    'postcode'    => $billingAddress->getData('postcode'),
                    'addresses'   => $this->getCollectionAddress($customer),
                );
                $data = array_merge($data, $customerarray);
            }

            if ($billingAddress) {
                $billingAddressArray = array(
                    'billing_firstname'  => $billingAddress->getData('firstname'),
                    'billing_lastname'   => $billingAddress->getData('lastname'),
                    'billing_city'       => $billingAddress->getData('city'),
                    'billing_street'     => $billingAddress->getData('street'),
                    'billing_country_id' => $billingAddress->getData('country_id'),
                    'billing_region'     => $billingAddress->getData('region'),
                    'billing_region_id'  => $billingAddress->getData('region_id'),
                    'billing_postcode'   => $billingAddress->getData('postcode'),
                    'billing_telephone'  => $billingAddress->getData('telephone'),
                );
                $data = array_merge($data, $billingAddressArray);
            }


            if ($shippingAddress) {
                $shippingAddressArray = array(
                    'shipping_firstname'  => $shippingAddress->getData('firstname'),
                    'shipping_lastname'   => $shippingAddress->getData('lastname'),
                    'shipping_city'       => $shippingAddress->getData('city'),
                    'shipping_street'     => $shippingAddress->getData('street'),
                    'shipping_country_id' => $shippingAddress->getData('country_id'),
                    'shipping_region'     => $shippingAddress->getData('region'),
                    'shipping_region_id'  => $shippingAddress->getData('region_id'),
                    'shipping_postcode'   => $shippingAddress->getData('postcode'),
                    'shipping_telephone'  => $shippingAddress->getData('telephone'),
                );
                $data = array_merge($data, $shippingAddressArray);
            }


        } else {
            $data = array(
                'id'          => $customer->getId(),
                'type'        => Mage::helper('adminhtml')->__('Customer'),
                'name'  => $sublogin->getFirstname().' '. $sublogin->getLastname(),
                'fname'   => $sublogin->getFirstname(),
                'lname'  => $sublogin->getLastname(),
                'email'     => $sublogin->getEmail(),
                'description' => $customer->getCompany(),
                'telephone' => $sublogin->getMobile_number(),
                'addresses'   => $this->getCollectionAddress($customer),
            );
        }

        $arr[] = $data;
    }}

    $this->setResults($arr);

    return $this;
}

控制器:

publicfunction customerSearchAction()
{
$items = array();

$start = $this->getRequest()->getParam('start', 1);
$limit = $this->getRequest()->getParam('limit', 10);
$query = $this->getRequest()->getParam('query', '');

//$searchInstance = new SM_XPos_Model_Search_Customer;
$searchInstance = Mage::getModel('xpos/search_customer');

$results = $searchInstance->setStart($start)
    ->setLimit($limit)
    ->setQuery($query)
    ->load()
    ->getResults();

$items = array_merge_recursive($items, $results);

$totalCount = sizeof($items);

$block = $this->getLayout()->createBlock('adminhtml/template')
    ->setTemplate('sm/xpos/index/customer/autocomplete.phtml')
    ->assign('items', $items);
$this->getResponse()->setBody($block->toHtml());
}

0 个答案:

没有答案