4

I want to save value in customer entity table in this table.

I created one custom field & I want to save value in custom field using controller.

But it is not working.

How is it possible?

Controller =>

<?php

namespace Abc\Def\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Magento\Customer\Model\Customer;

class Def extends Action
{
    protected $customerCollection;

    public function __construct(Context $context,PageFactory $resultPageFactory,Customer $customerCollection,\Magento\Customer\Api\CustomerRepositoryInterface $customerRepository)
    {
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
        $this->customerRepository = $customerRepository;
        $this->customerCollection = $customerCollection;
    }

    public function execute()
    {

        $randomletter = 1
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $customerSession = $objectManager->get('Magento\Customer\Model\Session');
        if($customerSession->isLoggedIn())
        {
            $customer = $this->customerRepository->getById($customerSession->getCustomerId());
            $customer->getCustomAttribute('custom_field');
            $customer->setCustomAttribute('custom_field',$randomletter);
            try
            {                
                $customer = $this->customerRepository->save($customer);
            }
            catch (Exception $e)
            {
                return $e->getMessage();
            }
        }
    }
}
5
  • have you created customer attribute or custom field?
    – Abdul
    May 23, 2017 at 4:56
  • 1
    yes ... @Abdul . I already created using install schema May 23, 2017 at 4:59
  • can you share your attribute code?
    – Abdul
    May 23, 2017 at 5:29
  • 1
    'custom_field' => [ 'type' => Table::TYPE_SMALLINT, 'nullable' => false, 'comment' => 'custom_field', ], May 23, 2017 at 5:37
  • Below solutions is working for you? May 23, 2017 at 12:59

2 Answers 2

1

Try this code:

<?php

namespace Abc\Def\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Magento\Customer\Model\Customer;

class Def extends Action
{
    protected $customerCollection;

    public function __construct(Context $context,PageFactory $resultPageFactory,Customer $customerCollection,\Magento\Customer\Api\CustomerRepositoryInterface $customerRepository)
    {
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
        $this->customerRepository = $customerRepository;
        $this->customerCollection = $customerCollection;
    }

    public function execute()
    {

        $randomletter = 1
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $customerSession = $objectManager->get('Magento\Customer\Model\Session');
        if($customerSession->isLoggedIn()) {
            $customer = $this->customerRepository->getById($customerSession->getCustomerId()); 
            //OR 
            //$customer = $customerSession->getCustomer();

            $customerData = $customer->getDataModel();
            try {
                $customerData->setCustomAttribute('custom_field', $randomletter);
                $customer->updateData($customerData);
                $customer->save();
            }
            catch (Exception $e)
            {
                return $e->getMessage();
            }
        }
    }
}
6
  • return error : Uncaught Error: Call to undefined method Magento\Customer\Model\Data\Customer::setCustomField() May 23, 2017 at 6:08
  • Updated ans pls check now.
    – Abdul
    May 23, 2017 at 9:34
  • Thanks for helping me....but it's not working... May 23, 2017 at 9:48
  • 1
    getDataModel() not working May 23, 2017 at 9:48
  • 1
    $customerData = $customer->getDataModel(); this row is redundant because repository returns Data Model. Without this row it should work.
    – zhartaunik
    Mar 12, 2018 at 11:18
1

Try this:

$customer = $this->customerRepository->getById($customerSession->getCustomerId());
$customer->setCustomAttribute('custom_field',$randomletter);
$customer->save();

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.