I have used below code in magento1 to get customer attributes.

$customerAccount = Mage::getConfig()->getFieldset('customer_account');
    foreach ($customerAccount as $code => $node) {
        if ($node->is('name')) {
            $fields[$code] = $code;
        }
    }

How the same code can be used in m2? Please anyone suggest me on this.

here is my full action method of m1.

  public function createPostAction()
{

    if (!$this->_getSession()->isLoggedIn()) {
        $this->_redirect('customer/account/login');
        return;
    }

    $this->_getSession()->setEscapeMessages(true);
    if ($this->getRequest()->isPost()) {
        $errors = array();

        $user = Mage::getModel('customer/customer')->setId(null);
        $master = Mage::getSingleton('customer/customer')->load($this->_getSession()->getCustomer()->getMasterId());
        $data = $this->getRequest()->getPost();


        $defaultFields = array();
        foreach (Mage::getConfig()->getFieldset('customer_account') as $code=>$node) {
            $defaultFields[] = $code;
            if ($node->is('create') && isset($data[$code])) {
                if ($code == 'email') {
                    $data[$code] = trim($data[$code]);
                }
                $user->setData($code, $data[$code]);
            }
        }
        foreach ($this->_getSharedFields() as $_att) {
            $user->setData($_att, $master->getData($_att));
        }

        try {
            $validationUser = $user->validate();
            if (is_array($validationUser)) {
                $errors = array_merge($validationUser, $errors);
            }
            $validationResult = count($errors) == 0;

            if (true === $validationResult) {

                $user->save();
                foreach ($this->_getSharedFields() as $_att) {
                    $user->setData($_att, $master->getData($_att));
                }
                $user->save();

                $message = $this->__('User was successfully created');
                $this->_getSession()->addSuccess($message);

                $this->sendNewSubUserAccountEmail($user);

                $this->_redirect('*/*/list');
                return;
            } else {
                $this->_getSession()->setCustomerFormData($this->getRequest()->getPost());
                if (is_array($errors)) {
                    foreach ($errors as $errorMessage) {
                        $this->_getSession()->addError($errorMessage);
                    }
                }
                else {
                    $this->_getSession()->addError($this->__('Invalid user data'));
                }
            }
        }
        catch (Mage_Core_Exception $e) {
            $this->_getSession()->setCustomerFormData($this->getRequest()->getPost());
            if ($e->getCode() === Mage_Customer_Model_Customer::EXCEPTION_EMAIL_EXISTS) {
                $url = Mage::getUrl('customer/account/forgotpassword');
                $message = $this->__('There is already an account with this emails address. If you are sure that it is your email address, <a href="%s">click here</a> to get the password and access your account.', $url);
                $this->_getSession()->setEscapeMessages(false);
            }
            else {
                $message = $e->getMessage();
            }
            $this->_getSession()->addError($message);
        }
        catch (Exception $e) {
            $this->_getSession()->setCustomerFormData($this->getRequest()->getPost())
                ->addException($e, $this->__('Can\'t save user'));
        }
    }
    $this->_redirectError(Mage::getUrl('*/*/new', array('_secure' => true)));
}

I am looking for code in magento2 for below one

 $user = Mage::getModel('customer/customer')->setId(null);

How this can be used in magento2?

有帮助吗?

解决方案

You can use below code for Magento-2

Add below dependency construct

protected $_fieldsetConfig;


    public function __construct(
        \Magento\Framework\DataObject\Copy\Config $fieldsetConfig
    ) {
        $this->_fieldsetConfig = $fieldsetConfig;
    }

Now you can get by below code

$customerAccount = $this->_fieldsetConfig->getFieldset('customer_account');
        foreach ($customerAccount as $code => $node) {
            if ($node->is('name')) {
                $fields[$code] = $code;
            }
        }

Reference from Magento Model class :

vendor/magento/module-customer/Model/ResourceModel/Customer/Collection.php

其他提示

for me it works like this:

$fieldset = $form->getElement('id_form');
许可以下: CC-BY-SA归因
scroll top