0

How to get database table prefix on install data? It is showing "table or view not found".

<?php

namespace ...;


use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
    /**
     * EAV setup factory
     *
     * @var EavSetupFactory
     */
    private $eavSetupFactory;

    /**
     * Init
     *
     * @param EavSetupFactory $eavSetupFactory
     */
    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();


            $setup->getConnection()->query("INSERT INTO `sales_order_status`(`status`, `label`) VALUES ('test','Test')");



        $setup->endSetup();
    }
}

It is giving error 'table or view not found'

1
  • How are you trying to get the table prefix? Please add some more info in your question? Oct 15, 2018 at 8:05

2 Answers 2

1

you can get the table name with prefix (if, any) like :

$setup->getTable('sales_order_status');

and use it in InstallData.php like below :

namespace ...;


use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
    /**
     * EAV setup factory
     *
     * @var EavSetupFactory
     */
private $eavSetupFactory;

/**
 * Init
 *
 * @param EavSetupFactory $eavSetupFactory
 */
public function __construct(EavSetupFactory $eavSetupFactory)
{
    $this->eavSetupFactory = $eavSetupFactory;
}

/**
 * {@inheritdoc}
 */
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
    $setup->startSetup();
    $tableName = $setup->getTable('sales_order_status');

    $setup->getConnection()->query('INSERT INTO `'.$tableName.'` (`status`, `label`) VALUES ('test','Test')');
    $setup->endSetup();
}
}
0
0

You can get table prefix using \Magento\Framework\App\DeploymentConfig.

First ,you have to inject the getTablePrefix \Magento\Framework\App\DeploymentConfig: on __construct() function then using this injection you can get prefix.

namespace {VendorName}\{ModuleName}\Setup;
use Magento\Framework\Config\ConfigOptionsListConstants;

class InstallSchema implements \Magento\Framework\Setup\InstallSchemaInterface
{

    /**
     * @var \Magento\Framework\App\DeploymentConfig
     */
    protected $deploymentConfig;

    public function __construct(
        \Magento\Framework\App\DeploymentConfig  $deploymentConfig
    ) {

        $this->deploymentConfig = $deploymentConfig;
    }
    public function getTablePrefix()
    {
        return $this->deploymentConfig->get(
                ConfigOptionsListConstants::CONFIG_PATH_DB_PREFIX
            );
    }
9
  • That is not working, I have created the method and called it to get the prefix but still no change, the database is not getting updated . Oct 15, 2018 at 9:21
  • What is your issue
    – Amit Bera
    Oct 15, 2018 at 9:25
  • Please explain property what you have issue with installer
    – Amit Bera
    Oct 15, 2018 at 9:26
  • Your question was table prefix
    – Amit Bera
    Oct 15, 2018 at 9:26
  • I am trying to add column to table 'sales_order' but it is giving error when I am trying to call "getTableName()" without database prefix, it is giving error 'table or view not found' Oct 15, 2018 at 9:32

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.