magento2: Injecting Product\Action into Console Command breaks setup:install
Preconditions (*)
- Magento 2.4.1 CE (reproducible on 2.4-develop)
- MariaDB 10.3
- PHP 7.4.13
- Linux 4.9.0-6-amd64 # 1 SMP Debian 4.9.82-1+deb9u3 (2018-03-02) x86_64 GNU/Linux
Inject \Magento\Catalog\Model\ResourceModel\Product\Action into a Console Command constructor, or get instance from Object Manager in constructor causes this error.
Steps to reproduce (*)
- Clean install magento
composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition magento 2.4.1 - Install my test module:
git clone https://git.jhodges.co.uk/jhodges/Magento_Test.git app/code/Test/Test/ - Try and install magento
bin/magento setup:install
Expected result (*)
- Magento installs
Actual result (*)
- Error is thrown:
[Progress: 4 / 1338]
Installing database schema:
In Mysql.php line 597:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'db.eav_entity_type' doesn't exist, query was: SELECT `main_table`.* FROM `eav_entity_type` AS `main_table` In Mysql.php line 110:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'db.eav_entity_type' doesn't exist, query was: SELECT `main_table`.* FROM `eav_entity_type` AS `main_table` In Mysql.php line 91:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'db.eav_entity_type' doesn't exist
Workaround
Workaround is to get instance of \Magento\Catalog\Model\ResourceModel\Product\Action from Object Manager in execute function.
Repeat the steps to reproduce above, but in step 2 clone the workaround branch of my test module git clone --branch workaround https://git.jhodges.co.uk/jhodges/Magento_Test.git app/code/Test/Test/
Then setup:install goes fine and you can confirm with bin/magento test:test should output Magento\Catalog\Model\ResourceModel\Product\Action\Interceptor
Logs:
[2020-12-26 15:09:56] main.ERROR: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'db.eav_entity_type' doesn't exist, query was: SELECT `main_table`.* FROM `eav_entity_type` AS `main_table`
#0 /var/www/magento/vendor/magento/framework/DB/Adapter/Pdo/Mysql.php(625): Magento\Framework\DB\Adapter\Pdo\Mysql->_query()
#1 /var/www/magento/generated/code/Magento/Framework/DB/Adapter/Pdo/Mysql/Interceptor.php(95): Magento\Framework\DB\Adapter\Pdo\Mysql->query()
#2 /var/www/magento/vendor/magento/zendframework1/library/Zend/Db/Adapter/Abstract.php(737): Magento\Framework\DB\Adapter\Pdo\Mysql\Interceptor->query()
#3 /var/www/magento/generated/code/Magento/Framework/DB/Adapter/Pdo/Mysql/Interceptor.php(1085): Zend_Db_Adapter_Abstract->fetchAll()
#4 /var/www/magento/vendor/magento/framework/Data/Collection/Db/FetchStrategy/Query.php(21): Magento\Framework\DB\Adapter\Pdo\Mysql\Interceptor->fetchAll()
#5 /var/www/magento/vendor/magento/framework/Data/Collection/AbstractDb.php(782): Magento\Framework\Data\Collection\Db\FetchStrategy\Query->fetchAll()
#6 /var/www/magento/vendor/magento/framework/Data/Collection/AbstractDb.php(677): Magento\Framework\Data\Collection\AbstractDb->_fetchAll()
#7 /var/www/magento/generated/code/Magento/Eav/Model/ResourceModel/Entity/Type/Collection/Interceptor.php(320): Magento\Framework\Data\Collection\AbstractDb->getData()
#8 /var/www/magento/vendor/magento/module-eav/Model/Config.php(389): Magento\Eav\Model\ResourceModel\Entity\Type\Collection\Interceptor->getData()
#9 /var/www/magento/vendor/magento/module-eav/Model/Config.php(428): Magento\Eav\Model\Config->_initEntityTypes()
#10 /var/www/magento/vendor/magento/module-eav/Model/Entity/AbstractEntity.php(322): Magento\Eav\Model\Config->getEntityType()
#11 /var/www/magento/generated/code/Magento/Catalog/Model/ResourceModel/Product/Action/Interceptor.php(86): Magento\Eav\Model\Entity\AbstractEntity->setType()
#12 /var/www/magento/vendor/magento/module-catalog/Model/ResourceModel/Product/Action.php(92): Magento\Catalog\Model\ResourceModel\Product\Action\Interceptor->setType()
#13 /var/www/magento/vendor/magento/framework/Model/ResourceModel/AbstractResource.php(42): Magento\Catalog\Model\ResourceModel\Product\Action->_construct()
#14 /var/www/magento/vendor/magento/module-eav/Model/Entity/AbstractEntity.php(245): Magento\Framework\Model\ResourceModel\AbstractResource->__construct()
#15 /var/www/magento/vendor/magento/module-catalog/Model/ResourceModel/AbstractResource.php(56): Magento\Eav\Model\Entity\AbstractEntity->__construct()
#16 /var/www/magento/vendor/magento/module-catalog/Model/ResourceModel/Product/Action.php(76): Magento\Catalog\Model\ResourceModel\AbstractResource->__construct()
#17 /var/www/magento/generated/code/Magento/Catalog/Model/ResourceModel/Product/Action/Interceptor.php(14): Magento\Catalog\Model\ResourceModel\Product\Action->__construct()
#18 /var/www/magento/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(121): Magento\Catalog\Model\ResourceModel\Product\Action\Interceptor->__construct()
#19 /var/www/magento/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php(66): Magento\Framework\ObjectManager\Factory\AbstractFactory->createObject()
#20 /var/www/magento/vendor/magento/framework/ObjectManager/ObjectManager.php(70): Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create()
#21 /var/www/magento/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(170): Magento\Framework\ObjectManager\ObjectManager->get()
#22 /var/www/magento/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(276): Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgument()
#23 /var/www/magento/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(239): Magento\Framework\ObjectManager\Factory\AbstractFactory->getResolvedArgument()
#24 /var/www/magento/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php(34): Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgumentsInRuntime()
#25 /var/www/magento/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php(59): Magento\Framework\ObjectManager\Factory\Dynamic\Developer->_resolveArguments()
#26 /var/www/magento/vendor/magento/framework/ObjectManager/ObjectManager.php(70): Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create()
#27 /var/www/magento/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(206): Magento\Framework\ObjectManager\ObjectManager->get()
#28 /var/www/magento/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(182): Magento\Framework\ObjectManager\Factory\AbstractFactory->parseArray()
#29 /var/www/magento/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(276): Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgument()
#30 /var/www/magento/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php(239): Magento\Framework\ObjectManager\Factory\AbstractFactory->getResolvedArgument()
#31 /var/www/magento/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php(34): Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgumentsInRuntime()
#32 /var/www/magento/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php(59): Magento\Framework\ObjectManager\Factory\Dynamic\Developer->_resolveArguments()
#33 /var/www/magento/vendor/magento/framework/ObjectManager/ObjectManager.php(56): Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create()
#34 /var/www/magento/setup/src/Magento/Setup/Model/ObjectManagerProvider.php(78): Magento\Framework\ObjectManager\ObjectManager->create()
#35 /var/www/magento/setup/src/Magento/Setup/Model/ObjectManagerProvider.php(64): Magento\Setup\Model\ObjectManagerProvider->createCliCommands()
#36 /var/www/magento/setup/src/Magento/Setup/Model/Installer.php(882): Magento\Setup\Model\ObjectManagerProvider->get()
#37 [internal function]: Magento\Setup\Model\Installer->installSchema()
#38 /var/www/magento/setup/src/Magento/Setup/Model/Installer.php(388): call_user_func_array()
#39 /var/www/magento/setup/src/Magento/Setup/Console/Command/InstallCommand.php(231): Magento\Setup\Model\Installer->install()
#40 /var/www/magento/vendor/symfony/console/Command/Command.php(255): Magento\Setup\Console\Command\InstallCommand->execute()
#41 /var/www/magento/vendor/symfony/console/Application.php(1009): Symfony\Component\Console\Command\Command->run()
#42 /var/www/magento/vendor/symfony/console/Application.php(273): Symfony\Component\Console\Application->doRunCommand()
#43 /var/www/magento/vendor/magento/framework/Console/Cli.php(115): Symfony\Component\Console\Application->doRun()
#44 /var/www/magento/vendor/symfony/console/Application.php(149): Magento\Framework\Console\Cli->doRun()
#45 /var/www/magento/bin/magento(23): Symfony\Component\Console\Application->run()
#46 {main} [] []
Please provide Severity assessment for the Issue as Reporter. This information will help during Confirmation and Issue triage processes.
- Severity: S0 - Affects critical data or functionality and leaves users without workaround.
- Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
- Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
- Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
- Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 17 (14 by maintainers)
Hello @bobemoe
Magento create commands objects before installing db tables. See:
Magento\Setup\Model\Installer::installSchema(array $request)and\Magento\Setup\Model\ObjectManagerProvider::createCliCommands().And in your case
\Magento\Catalog\Model\ResourceModel\Product\Actiontry to get connection to tables that not exists. Use proxy to pass object with a lot of dependencies or object that create connection with db in constructor, into Command classes. This will help avoid the use of unnecessary dependencies and invoking the database connection in the constructorUse Proxy to pass
\Magento\Catalog\Model\ResourceModel\Product\Actioninto Console command constructor.Example:
Hi @ihor-sviziev, yes! There is no work done on that internally.
@sidolov @gabrieldagama seems like the automatic comment is a mistake?
Yes! I can confirm using the Proxy class has resolved the issue.
There actually seems to be no need for
<sequence>inmodule.xml, although it sounds like this is a good idea anyway since my module does have dependencies.Thanks for the help, and I agree that the logs / error message could have been more helpful 😉
@engcom-Bravo thanks for taking another look and confirming 😃