1

I want to get id of the customer when I add new record of customer new account in customer_save_before event.

Id will be display before record saved in customer_entity table.

How to get id using customer_save_before in observer?

Please help me.

7
  • 1
    By increment +1 into last inserted id you may get your new ID May 18, 2017 at 7:25
  • 1
    @RonakChauhan when 100 people insert record at same time then which last id i got...there is no sufficient logic for that. May 18, 2017 at 7:33
  • 1
    @Satish I want to add mobile number in customer_entity table and I want to check that mobile number is repeated or not before save record. If repeat then error message display and stop record save otherwise add record May 18, 2017 at 7:35
  • 2
    Rohan Hapani, you should update ur question ... The target of your question and comment are not same. It make user to confusion
    – Amit Bera
    May 18, 2017 at 7:38
  • 1
    You should correct your question, as per your question you won't get any sufficient answer. May 18, 2017 at 8:52

2 Answers 2

6

As per as your comment, you want validate customer mobile no before have whenever new customer is created or record is updated.

In this you can use your customer_save_before event:

and observer may be like this:


<?php
namespace Devamitbera\MobileCheck\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\AlreadyExistsException;

class ValidateMobileNo implements ObserverInterface
{

    protected $_customerCollectionFactory;
    public function __construct(
        \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customerCollectionFactory,
    ) {
        $this->_customerCollectionFactory = $customerCollectionFactory;
    }


    /**
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $customer = $observer->getCustomer();
        $customerCollection= $this->_customerCollectionFactory->create();
        $customerCollection->addAttribuTeToFilter('[YOUR_ATTRUBUTE]',$customer->getData('[YOUR_ATTRUBUTE]'));
        if ($customer->getId()) {   
            $customerCollection->addAttribuTeToFilter('entity_id',array('neq' => (int)$customer->getId()));   
         }   
         if($customerCollection->getSize()>0){
             throw new AlreadyExistsException(
                __('A customer with the same Mobile already exists .')
            );

         }

    }

}
1

I have not tried it myself but dig into Magento core files and got this solution.

Please look into this class:

Magento\Framework\Model\ResourceModel\Db\AbstractDb

Into the function named saveNewObject() around line number 762

 $object->setId($this->getConnection()->lastInsertId($this->getMainTable()));

using the lastInsertId() one can get the last added Id in particular table by passing the name of the table.

Hope, this will help you.

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.