1

I need to initialize the object manager out of for each loop.

Here is my code:

<?php
namespace Abc\Mkt\Controller\Adminhtml\Order;
    use Magento\Framework\Stdlib\DateTime;
    use Magento\Framework\App\Config\ScopeConfigInterface;
    class Masssync extends \Magento\Backend\App\Action{
        /**
         * Masssync Action for Mass Sync of customer with Marketo 
         * @return Void   
         * */
        protected $_objectManager;
        protected $_api;
        protected $scopeConfig;
        public function __construct(Orderapi $Api, ScopeConfigInterface $scopeConfig, \Magento\Framework\Json\Helper\Data $jsonHelper, \Magento\Customer\Model\Customer $customerData,\Magento\Framework\ObjectManagerInterface $objectmanager, \Magento\Backend\App\Action\Context $context)
        {

            $this->scopeConfig = $scopeConfig;
            $this->jsonHelper = $jsonHelper;
            $this->dataHelper = $dataHelper; 
            $this->customerData = $customerData; 
            $this->modelData = $modelData; 
            $this->_objectManager = $objectmanager;
            $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
            parent::__construct($context);
        }
        public function execute(){

                    $customerIds = $this->getRequest()->getParam("selected");
                     foreach ($customerIds as $customerId) {
                                $item = $this->_objectManager->create('\Abc\Mkt\Model\Data');
                                $customer = $this->customerData->load($customerId);
                                $item->setCustomerId($customerId);
                                $item->setSyncWithMarketo(1);
                                $item->save();  
                                $data['FirstName'] = $customer->getFirstname();
                                $data['LastName'] = $customer->getLastname();
                                $data['Email'] = $customer->getEmail();         
                                $allfield = array("email");
                                array_push($mergeData,$data);
                        }

                }
        }

Please suggest

Thanks

2

Object manager is not recommended in Magento.
So the actual implementation should be like this:

protected $modelData;
public function __construct(
    Orderapi $Api,
    ScopeConfigInterface $scopeConfig,
    \Magento\Framework\Json\Helper\Data $jsonHelper,
    \Magento\Customer\Model\Customer $customerData,
    \Magento\Framework\ObjectManagerInterface $objectmanager,
    \Magento\Backend\App\Action\Context $context,
    \Abc\Mkt\Model\Data $modelData
)
{

    $this->scopeConfig = $scopeConfig;
    $this->jsonHelper = $jsonHelper;
    $this->dataHelper = $dataHelper;
    $this->customerData = $customerData;
    $this->modelData = $modelData;
    $this->_objectManager = $objectmanager;
    $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
    $this->modelData = $modelData;
    parent::__construct($context);
}


public function execute()
{
    $customerIds = $this->getRequest()->getParam("selected");
    foreach ($customerIds as $customerId) {
        $item = clone $this->modelData;
        $customer = $this->customerData->load($customerId);
        $item->setCustomerId($customerId);
        $item->setSyncWithMarketo(1);
        $item->save();  
        $data['FirstName'] = $customer->getFirstname();
        $data['LastName'] = $customer->getLastname();
        $data['Email'] = $customer->getEmail();         
        $allfield = array("email");
        array_push($mergeData,$data);
    }

}

But still, if you want to use object manager, you can do it by this:

$itemObj = $this->_objectManager->create('\Abc\Mkt\Model\Data');
foreach ($customerIds as $customerId)
{
    $item = clone $itemObj;
    $customer = $this->customerData->load($customerId);
    $item->setCustomerId($customerId);
    $item->setSyncWithMarketo(1);
    $item->save();  
    $data['FirstName'] = $customer->getFirstname();
    $data['LastName'] = $customer->getLastname();
    $data['Email'] = $customer->getEmail();         
    $allfield = array("email");
    array_push($mergeData,$data);
}

Hope this will resolve your issue.

  • This is working for me – Shomita Nov 18 '19 at 6:09
  • Can you please tell me? why we use clone @shoaib? – Shomita Nov 18 '19 at 6:12
  • 1
    Because you want to use same object for all the customers and you want to make new object every time, so clone will copy your object to new one and you can use it separately. – Shoaib Munir Nov 18 '19 at 6:31
  • +1 for answer acceptance :) – Shoaib Munir Nov 18 '19 at 6:31
  • 1
    Thankyou so much for clarification – Shomita Nov 18 '19 at 6:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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