If the wrong session is used, it is because the session_name is wrong. The session name is set in Mage_Core_Model_Session_Abstract_Varien::setSessionName()
.
But after a look on the Stacktrace, I have no idea, why overwriting _construct
or even __construct
should be a problem here:
#0 Mage_Core_Model_Session_Abstract_Varien->setSessionName(frontend) called at [app/code/core/Mage/Core/Model/Session/Abstract/Varien.php:111]
#1 Mage_Core_Model_Session_Abstract_Varien->start(frontend) called at [app/code/core/Mage/Core/Model/Session/Abstract/Varien.php:166]
#2 Mage_Core_Model_Session_Abstract_Varien->init(core, frontend) called at [app/code/core/Mage/Core/Model/Session/Abstract.php:84]
#3 Mage_Core_Model_Session_Abstract->init(core, frontend) called at [app/code/core/Mage/Core/Model/Session.php:42]
#4 Mage_Core_Model_Session->__construct(Array ([name] => frontend)) called at [app/code/core/Mage/Core/Model/Config.php:1348]
#5 Mage_Core_Model_Config->getModelInstance(core/session, Array ([name] => frontend)) called at [app/Mage.php:462]
#6 Mage::getModel(core/session, Array ([name] => frontend)) called at [app/Mage.php:476]
#7 Mage::getSingleton(core/session, Array ([name] => frontend)) called at [app/code/core/Mage/Core/Controller/Varien/Action.php:494]
#8 Mage_Core_Controller_Varien_Action->preDispatch() called at [app/code/core/Mage/Core/Controller/Front/Action.php:64]
#9 Mage_Core_Controller_Front_Action->preDispatch() called at [app/code/core/Mage/Core/Controller/Varien/Action.php:408]
#10 Mage_Core_Controller_Varien_Action->dispatch(view) called at [app/code/core/Mage/Core/Controller/Varien/Router/Standard.php:250]
#11 Mage_Core_Controller_Varien_Router_Standard->match(Mage_Core_Controller_Request_Http Object ([] => /test.html,[] => ,[] => /test.html,[] => ,[] => ,[] => Array (),[] => catalog,[] => ,[] => Mage_Catalog,[] => ,[] => Array (),[] => Array ([0] => _GET,[1] => _POST),[] => /catalog/product/view/id/21,[] => ,[] => ,[] => catalog/product/view/id/21,[] => Array ([id] => 21),[] => ,[] => Array ([rewrite_request_path] => test.html),[] => 1,[] => catalog,[] => module,[] => product,[] => controller,[] => view,[] => action)) called at [app/code/core/Mage/Core/Controller/Varien/Front.php:176]
#12 Mage_Core_Controller_Varien_Front->dispatch() called at [app/code/core/Mage/Core/Model/App.php:354]
#13 Mage_Core_Model_App->run(Array ([scope_code] => ,[scope_type] => store,[options] => Array ())) called at [app/Mage.php:683]
#14 Mage::run(, store) called at [index.php:87]
So I found out the problem with the customer session was that I was instantiating my own custom session way to early. As I learned from this article by Alan Storm, instantiating sessions too early can break session storage, so different pages where accessing different sessions. That's why sometimes I didn't get the correct customer session data.
I was trying to use a session object in a custom router and found it's a big NO. I changed my router code to instead set a POST
param to pass on my value and then store it to the session later on in the process.
P.S. My custom session is still buggy, since it appears its data is only accessible in the page request that set it and not any other subsequent requests, but the customer session is okay now so that's half the problem gone.
Best Answer
There are two ways of creating object:
1) Model
\Magento\Customer\Model\Session
2) Factory
\Magento\Customer\Model\SessionFactory
If you use Model class, it will always create shared objects it will not create a new object, but what if you want to use new object of this class, use factory method
Factory method will be used to instantiate an object. In Magento 2, the Magento 2 Factory Object do the same thing. The Factory class name is the name of Model class and append with the Factory word.
So for example, we will have SessionFactory class. You must not create this class. Magento will create it for you. Whenever Magento’s object manager encounters a class name that ends in the word ‘Factory’, it will automatically generate the Factory class in the var/generation folder if the class does not already exist.
This factory class is also dependent on the ObjectManager class. When you will load the class or run magento2 compilation using CLI:
Before Magento 2.2
var/generation/{Vendor}/{Module}/Model/MyModelFactory.php
After Magento 2.2
generated/{Vendor}/{Module}/Model/MyModelFactory.php
You should always use factories for non-injectable objects, All the entities such as Product, Customer are non-injectable and classes such as EventManager and all the Management classes are injectable in magento2.
Injectable objects don’t have identities but in the case of non-injectable objects they have identities, so you don’t know which instance you need at which time, so you must use Factory classes to inject non-injectable objects.
Here is the example factory class that is generated for
Magento\Customer\Model\Customer
, insidevar/generation/Magento/Customer/Model/CustomerFactory
:Source: webkul, Google