public function testGetAttributeMetadata() { $attribute = new \Magento\Framework\DataObject(['entity_type_id' => '1', 'attribute_id' => '2', 'backend' => new \Magento\Framework\DataObject(['table' => 'customer_entity_varchar']), 'backend_type' => 'varchar']); $this->eavConfig->expects($this->once())->method('getAttribute')->will($this->returnValue($attribute)); $result = $this->helper->getAttributeMetadata('customer', 'lastname'); $expected = ['entity_type_id' => '1', 'attribute_id' => '2', 'attribute_table' => 'customer_entity_varchar', 'backend_type' => 'varchar']; foreach ($result as $key => $value) { $this->assertArrayHasKey($key, $expected, 'Attribute metadata with key "' . $key . '" not found.'); $this->assertEquals($expected[$key], $value, 'Attribute metadata with key "' . $key . '" has invalid value.'); } }
/** * Add customer details(email, firstname, lastname) to select * * @return $this */ public function addCustomerDetails() { $select = $this->getSelect()->joinInner(array('ce' => $this->getTable('customer_entity')), 'ce.entity_id = main_table.customer_id', array('customer_email' => 'email')); $adapter = $this->getConnection(); $firstNameMetadata = $this->_eavHelper->getAttributeMetadata(CustomerMetadataServiceInterface::ENTITY_TYPE_CUSTOMER, 'firstname'); $joinExpr = 'firstname.entity_id = main_table.customer_id AND ' . $adapter->quoteInto('firstname.entity_type_id = ?', $firstNameMetadata['entity_type_id']) . ' AND ' . $adapter->quoteInto('firstname.attribute_id = ?', $firstNameMetadata['attribute_id']); $select->joinLeft(array('firstname' => $firstNameMetadata['attribute_table']), $joinExpr, array('customer_firstname' => 'value')); $lastNameMetadata = $this->_eavHelper->getAttributeMetadata(CustomerMetadataServiceInterface::ENTITY_TYPE_CUSTOMER, 'lastname'); $joinExpr = 'lastname.entity_id = main_table.customer_id AND ' . $adapter->quoteInto('lastname.entity_type_id = ?', $lastNameMetadata['entity_type_id']) . ' AND ' . $adapter->quoteInto('lastname.attribute_id = ?', $lastNameMetadata['attribute_id']); $select->joinLeft(array('lastname' => $lastNameMetadata['attribute_table']), $joinExpr, array('customer_lastname' => 'value')); return $this; }
/** * Add Customer data to collection * * @return $this */ public function addCustomerData() { // alias => attribute_code $attributes = ['customer_lastname' => 'lastname', 'customer_firstname' => 'firstname', 'customer_email' => 'email']; foreach ($attributes as $alias => $attributeCode) { $attribute = $this->_eavHelper->getAttributeMetadata(\Magento\Customer\Api\CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, $attributeCode); $tableAlias = 'customer_' . $attributeCode; if ($attribute['backend_type'] == 'static') { $this->getSelect()->joinLeft([$tableAlias => $attribute['attribute_table']], sprintf('%s.entity_id=main_table.customer_id', $tableAlias), [$alias => $attributeCode]); $this->_fields[$alias] = sprintf('%s.%s', $tableAlias, $attributeCode); } else { $joinConds = [sprintf('%s.entity_id=main_table.customer_id', $tableAlias), $this->getConnection()->quoteInto($tableAlias . '.attribute_id=?', $attribute['attribute_id'])]; $this->getSelect()->joinLeft([$tableAlias => $attribute['attribute_table']], join(' AND ', $joinConds), [$alias => 'value']); $this->_fields[$alias] = sprintf('%s.value', $tableAlias); } } $this->setFlag('has_customer_data', true); return $this; }
/** * Adds customer info to select * * @return $this */ public function showCustomerInfo() { $adapter = $this->getConnection(); $lastNameData = $this->_customerHelperData->getAttributeMetadata(CustomerMetadataService::ENTITY_TYPE_CUSTOMER, 'lastname'); $firstNameData = $this->_customerHelperData->getAttributeMetadata(CustomerMetadataService::ENTITY_TYPE_CUSTOMER, 'firstname'); $this->getSelect()->joinLeft(array('customer_lastname_table' => $lastNameData['attribute_table']), $adapter->quoteInto('customer_lastname_table.entity_id=main_table.customer_id AND customer_lastname_table.attribute_id = ?', (int) $lastNameData['attribute_id']), array('customer_lastname' => 'value'))->joinLeft(array('customer_firstname_table' => $firstNameData['attribute_table']), $adapter->quoteInto('customer_firstname_table.entity_id=main_table.customer_id AND customer_firstname_table.attribute_id = ?', (int) $firstNameData['attribute_id']), array('customer_firstname' => 'value')); return $this; }