3

I follow this tutorial: https://www.pierrefay.com/en/magento2-training/install-upgrade-setup.html but i didn't add table into database, i had do anything in tutorial. From here: Go to /app/code/Pfay/Contacts/etc/module.xml and verify that the version of our module magento2 is 0.1.0.

<module name="Pfay_Contacts" setup_version="0.1.0">

Creating our installation process for magento2 Now we will create the file that will be launched during the installation of our module, under magento it is always called in the same way: InstallSchema and when called it executes the install function. Create the file /app/code/Pfay/Contacts/Setup/InstallSchema.php like this:

<?php

namespace Pfay\Contacts\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

/**
 * @codeCoverageIgnore
 */
class InstallSchema implements InstallSchemaInterface
{
    /**
     * @param SchemaSetupInterface $setup
     * @param ModuleContextInterface $context
     * @throws \Zend_Db_Exception
     */

    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
        /**
         * Create table 'pfay_contacts'
         */

        if (!$setup->getConnection()->isTableExists($setup->getTable('pfay_contacts'))) {
            $table = $setup->getConnection()
                ->newTable($setup->getTable('pfay_contacts'))
                ->addColumn(
                    'pfay_contacts_id',
                    \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                    null,
                    ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
                    'Contacts ID'
                )
                ->addColumn(
                    'name',
                    \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    100,
                    ['nullable' => false, 'default' => 'simple'],
                    'Name'
                )
                ->addColumn(
                    'email',
                    \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    100,
                    ['nullable' => false, 'default' => 'simple'],
                    'Email'
                )
                ->setComment('Pfay Contacts Table')
                ->setOption('type', 'InnoDB')
                ->setOption('charset', 'utf8');

            $setup->getConnection()->createTable($table);
        }
        $setup->endSetup();
    }
}

In this setup, we create the table in database that will be used for the continuation of the tutorials.

You notice the getTable function that just makes sure to format the table name, no need to declare pfay_contacts elsewhere (as on magento1). This creates the table with the newTable function and adds columns with addColumn , a comment with setComment And we specify options linked to the database with setOption The table is then stored in the database from the connection and the createTable function.

As for magento1, an installer starts with startSetup and ends with endSetup

Enable the module, update the tables and assign the rights to the cache Via the following commands:

php bin/magento module:enable Pfay_Contacts
php bin/magento setup:upgrade 
chmod 777 -R var/

Can anyone help me with this?

  • have you check your module actually enable or not ? go to the app/etc/config.php and check your module is listed there? with value 1 . Also first run setup:upgrade and then run module:enable commands – Manthan Dave Jan 23 '17 at 8:17
  • Use UpgradeSchema if module save in core_setup or remove your module entry from core_setup – Vaibhav Ahalpara Jan 23 '17 at 8:22
  • my module is enable, and i run command but not work – Nguyễn Hồng Quân Jan 23 '17 at 8:28
3

That is better if we take a look at the database after running setup upgrade command. If our custom table cannot be added to database. We need to check again.

  • Remember to remove the setup module from table setup_module.
  • Run setup upgrade command again.

You should read more: https://magento.stackexchange.com/a/150187/33057. The install data script of module only affect your database when running setup command at the first time to set up the module. Changing the setup version number in module xml doesn't apply for install script also. Need to remove setup_module and try again.

There is a note: you also check the existing table $setup->getConnection()->isTableExists($setup->getTable('pfay_contacts')). The new table will not be created if there is the existing one.

1

It looks like you has upgraded your module before writing installer script.

First, remove your module from setup_module table, then try to upgrade using below cli command.

php bin/magento module:enable Pfay_Contacts

php bin/magento setup:upgarde

php bin/magento setup:di:compile

  • thanks you! i have remove in setup_module and upgrade then run! – Nguyễn Hồng Quân Jan 23 '17 at 8:57
  • have you checked your custom table in the database? – chirag Jan 23 '17 at 11:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.