cancel
Showing results for 
Search instead for 
Did you mean: 

The customer is loaded even if no customer's ID in session

0 Kudos

The customer is loaded even if no customer's ID in session

Feature request from jacquesbh, posted on GitHub Jul 23, 2016

I'm playing with performances issues.

One of them (too much to write an issue for each) is that the Customer is loaded on each page, event if the customer isn't logged in!

There is no test about the possibility of having a customer's ID equal to null in the source code.

Preconditions

  1. Magento EE 2.0.2 (but doesn't matter, it is in CE too…)
  2. Nginx / PHP5-fpm

Steps to reproduce

The problem is there: https://github.com/magento/magento2/blob/2d368d0134597257ef5697a4bcbb7f69f2aebed8/app/code/Magento/C...

If you want to test this just add this before the line 288 on the file above:

syslog(7, 'Value of the customer\'s ID is ' . var_export($this->getCustomerId(), true));

Which results in:

    /**
     * Retrieve customer model object
     *
     * @return Customer
     * use getCustomerId() instead
     */
    public function getCustomer()
    {
        if ($this->_customerModel === null) {
            syslog(7, 'Value of the customer\'s ID is ' . var_export($this->getCustomerId(), true));
            $this->_customerModel = $this->_customerFactory->create()->load($this->getCustomerId());
        }
        return $this->_customerModel;
    }

Expected result

  1. If the customer is logged in: look your syslog and you should get something like Jul 23 16:38:16 magento2ee: Value of the customer's ID is 1
  2. If the customer isn't logged in: you shouldn't get any log because a customer with the ID null shouldn't be loaded.

Actual result

  1. We always get a log… and specially a Value of the customer's ID is NULL if the customer isn't logged in.
5 Comments
apiuser
New Member

Comment from antonkril, posted on GitHub Jul 25, 2016

Good catch, @jacquesbh.

The bigger issue we're moving from is that we're loading customer session. We did couple code modifications that bring us closer to the state when we will be able to not load customer session on generic data requests (not private data) at all.

We will investigate your scenario of customer loading.

apiuser
New Member

Comment from jacquesbh, posted on GitHub Jul 25, 2016

Here a simple if should do the job I think.

    /**
     * Retrieve customer model object
     *
     * @return Customer
     * use getCustomerId() instead
     */
    public function getCustomer()
    {
        if ($this->_customerModel === null) {
            $this->_customerModel = $this->_customerFactory->create();
            if ($this->getCustomerId()) {
                // Load the customer if we have it's ID, and only in that case
                $this->_customerModel->load($this->getCustomerId());
            }
        }
        return $this->_customerModel;
    }

It seems that this simple condition should do the job because in the case we load a customer with an id null, it should result as the same customer without loading… right?

apiuser
New Member

Comment from kandy, posted on GitHub Jul 25, 2016

I believe that it is error when getCustomer method called when getCustomerId == null

apiuser
New Member

Comment from jacquesbh, posted on GitHub Jul 25, 2016

Sorry @kandy but I don't see where the error is. Can you enlight me?

apiuser
New Member

Comment from piotrekkaminski, posted on GitHub Jul 25, 2016

cc @choukalos