1

Can anyone help me how to change the object manager to a constructor in magento2:

$om = \Magento\Framework\App\ObjectManager::getInstance();
            $customerSession = $om->get('Magento\Customer\Model\Session');
            $customerData = $customerSession->getCustomer()->getData();
            $customerId = $customerSession->getCustomerId();
            $customerObj = $om->create('Magento\Customer\Model\Customer')
                ->load($customerId);
            $customerData = $customerObj->getDataModel();
    try {
                    $customerData->setCustomAttribute('r_what_percent_of_the_pharmacy', $r_what_percent_of_the_pharmacy);
                    $customerData->setCustomAttribute('r_what_percent_of_prescrip', $r_what_percent_of_prescrip);
                    $customerData->setCustomAttribute('r_how_many_total_prescriptions', $r_how_many_total_prescriptions);
                    $customerData->setCustomAttribute('r_has_the_owner_or_pharmacy', $r_has_the_owner_or_pharmacy);
                    $customerData->setCustomAttribute('r_does_the_pharmacy_solicit', $r_does_the_pharmacy_solicit);
                    $customerData->setCustomAttribute('r_is_your_clinic_or_pharmacy', $r_is_your_clinic_or_pharmacy);
                    $customerData->setCustomAttribute('r_are_one_or_more_practiti', $r_are_one_or_more_practiti);
                    $customerData->setCustomAttribute('r_does_the_pharmacy_offer', $r_does_the_pharmacy_offer);
                    $customerData->setCustomAttribute('r_any_exceptions', $r_any_exceptions);
                    $customerData->setCustomAttribute('r_any_exceptions_circumstances', $r_any_exceptions_circumstances);
                    $customerData->setCustomAttribute('r_does_the_pharmacy_clinic', $r_does_the_pharmacy_clinic);
                    $customerData->setCustomAttribute('r_currentstep', $r_currentstep);
                    $customerObj->updateData($customerData);
                    $customerObj->save();
                } catch (Exception $e) {
                    return $e->getMessage();
                }
3
  • 1
    Do you want to inject objectManager in constructor? May 18, 2018 at 7:21
  • where you have add this code.Please share full code
    – Amit Bera
    May 18, 2018 at 7:24
  • I want to use the constructor for a standard code of magento2 May 18, 2018 at 7:36

2 Answers 2

0

If you want to inject ObjectManager in constractor in your class, you can do following way:

use Magento\Framework\ObjectManagerInterface;
class YourClassName 
{

protected $objectManager;


/**
 * @param ObjectManagerInterface $objectManager
 */
public function __construct(
    ObjectManagerInterface $objectManager
) {
    $this->objectManager = $objectManager;
  }

public function yourFunctionName(){
    $customerSession = $this->objectManager->get('Magento\Customer\Model\Session');
    $customerData = $customerSession->getCustomer()->getData();
    $customerId = $customerSession->getCustomerId();
    $customerObj = $this->objectManager->create('Magento\Customer\Model\Customer')
        ->load($customerId);
    $customerData = $customerObj->getDataModel();
    try {
            $customerData->setCustomAttribute('r_what_percent_of_the_pharmacy', $r_what_percent_of_the_pharmacy);
            $customerData->setCustomAttribute('r_what_percent_of_prescrip', $r_what_percent_of_prescrip);
            $customerData->setCustomAttribute('r_how_many_total_prescriptions', $r_how_many_total_prescriptions);
            $customerData->setCustomAttribute('r_has_the_owner_or_pharmacy', $r_has_the_owner_or_pharmacy);
            $customerData->setCustomAttribute('r_does_the_pharmacy_solicit', $r_does_the_pharmacy_solicit);
            $customerData->setCustomAttribute('r_is_your_clinic_or_pharmacy', $r_is_your_clinic_or_pharmacy);
            $customerData->setCustomAttribute('r_are_one_or_more_practiti', $r_are_one_or_more_practiti);
            $customerData->setCustomAttribute('r_does_the_pharmacy_offer', $r_does_the_pharmacy_offer);
            $customerData->setCustomAttribute('r_any_exceptions', $r_any_exceptions);
            $customerData->setCustomAttribute('r_any_exceptions_circumstances', $r_any_exceptions_circumstances);
            $customerData->setCustomAttribute('r_does_the_pharmacy_clinic', $r_does_the_pharmacy_clinic);
            $customerData->setCustomAttribute('r_currentstep', $r_currentstep);
            $customerObj->updateData($customerData);
            $customerObj->save();
        } catch (Exception $e) {
            return $e->getMessage();
        }
 }  
}
0

I think what you ask is simply to convert "object manager code" to proper "constructor injection code".

Injecting ObjectManager in constructor is rare and mostly used for factories. Don't do that.

For most cases, just create a constructor like this

public function __construct(  
) {

}

and add all classes you need to it like this:

/** @var \Magento\Customer\Model\Session */
protected $session;

/** @var \Magento\Customer\Model\Customer */
protected $customer;

public function __construct(
    \Magento\Customer\Model\Session $session,
    \Magento\Customer\Model\Customer $customer
) {
    $this->session = $session;
    $this->customer = $customer;
}

Magento 2 automatically creates instances of the specified classes for you.

In yourFunction name, you can then use these objects like this (just an example):

public function yourFunctionName(){
    $customerId = $this->session->getCustomerId();
    $customerObj = $this->customer->load($customerId);
    $customerData = $customerObj->getDataModel();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.