-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Calling Api Interface from Module Install triggers Exception that Area Code is not set #1405
Comments
@Vinai , good point.
|
Thanks for your reply @buskamuza! TD;DR
The following is roughly my thought trail how I came to this opinion.
Regarding the sessions, none of the current commands require an active one. Regarding the area, I think adminhtml does seem like good choice as a default. Commands that require a different area to be set are few and should take care of initializing that area themselves. If it is documented that CLI scripts default to the adminhtml area, then I think it won't be a big issue if frontend events are not triggered during setup:upgrade execution. |
@Vinai , thanks for so detailed proposal! |
Hello @buskamuza do you have an update or workaround for creating CLI scripts that interact with the application ? Im currently trying to develop some CLI functionality but facing the same issues as @Vinai has described. |
Hi @jamescowie , for now I'd recommend to use a workaround suggested in #1225 (comment) |
Internal ticket MAGETWO-37862 |
I am experiencing this issue with Magento 2.1.0 for the service layer API Magento\Tax\Api\TaxRuleRepositoryInterface:create() (which @Vinai already described is part of his issue). |
+1 |
Any news in this matter? It's been 2 years and still no area code is set when running bin/magento. Writting any custom command that deals with access to data via repositories (ie. |
Working on this ticket #MLAU18 |
Spoke with @maksek the agreed approach likely to be manually declaring in execute() in custom class that extends Command:
A good approach is for custom class to specifically set area code in which this Command operates in, and only set area code if your class performs data operations using any of the api repository classes |
@btwq the problem is if some other command defines area code in constructor you will get exception. so it's better to use
And you can decide if area code is different than what you want either emulate or exit with message |
Hi @btwq thank you for research. Closing the issue |
@hatimeria-artur-jewula we discussed whether the try block is required. It makes sense however you run the risk of declaring a different area code than intended. e.g.
If (2) was in try block, then setAreaCode in (2) is ignored, and now you have set area as FRONTEND instead of ADMIN. By avoiding a try block, the developer is then forced to figure out where and what AreaCode is set. |
I've run into a problem while writing a small module to play some more with the service layer API.
I'm trying to add attribute options via
Magento\Eav\Api\AttributeOptionManagementInterface::add()
.This fails because no area is set while running
bin/magento setup:upgrade
.Not sure if I'm doing anything wrong. I would expect this to work, since the setup class is using part of the "official" exposed interface of the Magento_Eav module.
Here is the call stack as far as I can see.
Magento\Eav\Api\AttributeOptionManagementInterface::add()
causes the eventcatalog_entity_attribute_save_before
to be dispatched as follows:The resource model in this case is an instance of
Magento\Eav\Model\Resource\Entity\Attribute
This event has an observer registered, namely
Magento\Weee\Model\Observer
.What follows now is a series of dependency resolutions by the object manager.
Magento\Weee\Model\Observer
depends onMagento\Tax\Helper\Data
Magento\Tax\Helper\Data
depends onMagento\Tax\Api\TaxCalculationInterface
The preference for
Magento\Tax\Api\TaxCalculationInterface
isMagento\Tax\Model\TaxCalculation
Magento\Tax\Model\TaxCalculation
is dependent onMagento\Tax\Model\Calculation
Magento\Tax\Model\Calculation
is dependent onMagento\Customer\Model\Session
Magento\Customer\Model\Session
is dependent onMagento\Framework\Session\Generic
Magento\Framework\Session\Generic
extends from\Magento\Framework\Session\SessionManager
Magento\Framework\Session\SessionManager
depends on\Magento\Framework\App\State
So far so good, but in the constructor the session manager calls
$this->start()
:Magento\Framework\Session\SessionManager::__construct()
calls$this->start()
Magento\Framework\Session\SessionManager::start()
callsMagento\Framework\App\State::getAreaCode()
Here is the method in question:
Now the problem is that
$this->_areaCode
is not set.Summary: When running
bin/magento
the area code is not set on the app state instance.This causes an exception while the session is started.
Not sure where the bug lies: the session being started while
bin/magento
is being executed from the command line or the app area not being set.I can imagine the same problem cropping up in different API service layer calls, too, since this seems to be a low level issue.
Adding the attribute options via the eav setup class, or via attribute models and collections works like a charm, however I'm doing the exercise to practice how to do things the right way and use the service layer instead.
PS: In case it's helpful, I've pushed the code that calls the API service layer to github: https://github.com/vinai/VinaiKopp_EavOptionSetup
If you think it is useful I'd be happy to add the module install that triggers the error to this issue thread, too.
The text was updated successfully, but these errors were encountered: