2

I'm tried create Setup/InstallSchema.php in magento2 but it not work. So any so any suggestion? thanks. This file InstallSchema.php

    <?php
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Xanka\Slider\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
/**
 * @codeCoverageIgnore
 */
class InstallSchema implements InstallSchemaInterface
{
    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;

        $installer->startSetup();

        /**
         * Create table 'slideshow'
         */     
        $table = $installer->getConnection()
            ->newTable($installer->getTable('slideshow'))
            ->addColumn(
                'slideshow_id',
                \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                null,
                ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
                'Slideshow ID'
            )       
            ->addColumn(
                'name',
                \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                32,
                ['nullable' => false],
                'Name'
            )
            ->addColumn(
                'created_at',
                \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
                null,
                [],
                'Creation Time'
            )
            ->addColumn(
                'updated_at',
                \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
                null,
                [],
                'Updatetion Time'
            )              
            ->setComment('Slideshow Table');
        $installer->getConnection()->createTable($table);

        /**
         * Create table 'slidewshow_data'
         */
        $table = $installer->getConnection()
            ->newTable($installer->getTable('slideshow_data'))
            ->addColumn(
                'entity_id',
                \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                null,
                ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
                'Entity ID'
            )
            ->addColumn(
                'slideshow_id',
                \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                 null,
                 ['nullable' => false],
                'Slideshow ID'
            )
             ->addColumn(
                'name',
                \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                32,
                ['nullable' => false],
                'Name'
            )
             ->addColumn(
                'image',
                \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                255,
                ['nullable' => false],
                'Image Url'
            )
             ->addColumn(
                'description',
                \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                255,
                [],
                'Descripntion'
            )
            ->addColumn(
            'created_time',
            \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
                null,
                [],
                'Creation Time'
            )->addColumn(
                'updated_time',
                \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
                null,
                [],
                'Modification Time'
            ) 
            ->addIndex(
                $installer->getIdxName('slideshow_data', ['slideshow_id']),
                ['slideshow_id']
            )
            ->addForeignKey(
                $installer->getFkName('slideshow_data', 'slideshow_id', 'slideshow', 'slideshow_id'),
                'slideshow_id',
                $installer->getTable('slideshow'),
                'slideshow_id',
                \Magento\Framework\DB\Ddl\Table::ACTION_CASCADE
            )           
            ->setComment('Slideshow Data Table');                 
        $installer->getConnection() ->createTable($table);
        $installer->endSetup();

    }
}

3 Answers 3

3

In Magento 2 setup/update scripts do not run automatic. Please read documentation, how to enable/setup module

Also you may need modify/delete record in setup_module table in case if you need to rerun setup/upgrade script, because magento do not re run in case when the schema_version attribute from module.xml file is exists with same version in this table

3
  • Hi @KAndy, After i delete table in database and modify file InstallSchema.php. After that Enter the following at the command line: php -f bin/magento setup:upgrade. when i'm check in database then table not create. You can help ? thank you!
    – xanka
    Commented Oct 7, 2015 at 1:30
  • Thank @Ahmed. It ok. you can help answer question on?
    – xanka
    Commented Oct 7, 2015 at 1:36
  • try remove record about you module from setup_module table and run bin/magento setup:upgrade
    – KAndy
    Commented Oct 7, 2015 at 13:49
1

To upgrade module we need to create UpgradeSchema.php in Setup folder
1. change the Version in module.xml that is
setup_version="2.0.0" to setup_version="2.0.1"
2. in Setup folder create file called UpgradeSchema.php

<?php

namespace Sugarcode\Test\Setup;

use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\DB\Ddl\Table;

class UpgradeSchema implements UpgradeSchemaInterface
{
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
         $tableName = $setup->getTable('testtable');
        if (version_compare($context->getVersion(), '2.0.0') < 0) {
            // Changes here.
        }

        if (version_compare($context->getVersion(), '2.0.1', '<')) {

            // Changes here.
        }
        if (version_compare($context->getVersion(), '2.0.2', '<')) {
              if ($setup->getConnection()->isTableExists($tableName) == true) {
                $connection = $setup->getConnection();
                /* $connection->addColumn(
                    $tableName,
                    'updated_at',
                    ['type' => Table::TYPE_DATETIME,'nullable' => false, 'default' => '', 'afters' => 'created_at'],
                    'Updated At'
                ); */
                $connection->changeColumn(
                    $tableName,
                    'summary',
                    'short_summary',
                    ['type' => Table::TYPE_TEXT, 'nullable' => false, 'default' => ''],
                    'Short Summary'
                );
                // Changes here.
            }
        }
        if (version_compare($context->getVersion(), '2.0.3', '<')) {
              if ($setup->getConnection()->isTableExists($tableName) == true) {
                $connection = $setup->getConnection();

                  // Declare data
                $columns = [
                    'updated_at' => [
                        'type' => Table::TYPE_DATETIME,
                        'nullable' => false,
                        'comment' => 'Updated At',
                        'after' => 'created_at',
                    ],
                ];

                $connection = $setup->getConnection();
                foreach ($columns as $name => $definition) {
                    $connection->addColumn($tableName, $name, $definition);
                }

                // Changes here.
            }
        }
        $setup->endSetup();

    }
}

and then run upgrade command that is

php D:\xampp\htdocs\magento2a\bin\magento setup:upgrade

below are the few important function

public function addColumn($tableName, $columnName, $definition, $schemaName = null)
    public function changeColumn($tableName, $oldColumnName, $newColumnName, $definition, $flushData = false, $schemaName = null)
    public function modifyColumn($tableName, $columnName, $definition, $flushData = false, $schemaName = null)
    public function dropColumn($tableName, $columnName, $schemaName = null)
0

You haven't written a default value for

->addColumn(
     'slideshow_id',
      \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
       ['nullable' => false],
       'Slideshow ID'
 ) 

in slideshow_data , try changing it to

->addColumn(
     'slideshow_id',
      \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
       null,
       ['nullable' => false],
       'Slideshow ID'
 ) 

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.