1

I need to change the existing column (title to be null) in review_detail table in Review module in magento.Please provide me a solution

2
  • 1
    changing the column mean you want alter the table, ex. column name A to B ? Dec 18, 2017 at 6:20
  • No, I need to alter the title field to be null
    – Jaisa
    Dec 18, 2017 at 6:23

2 Answers 2

6

You can Modify the definition by upgrade script as below.

<?php

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

class UpgradeSchema implements UpgradeSchemaInterface
{

/**
 * Upgrades DB schema for a module
 *
 * @param SchemaSetupInterface $setup
 * @param ModuleContextInterface $context
 * @return void
 */
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
    $installer = $setup;
    $installer->startSetup();
    if ($installer->getConnection()->tableColumnExists('table_name', 'column_name')){
        $definition = [
            'type' => Table::TYPE_TEXT,
            'nullable' => true,
            'comment' => 'Comment'
        ];
        $installer->getConnection()->modifyColumn(
            $setup->getTable('table_name'),
            'column_name',
            $definition
        );
    }
}
}

Note: I would suggest you not to change the definition of column of core table, in your case you can pass the empty string as title which should work.

7
  • Thank you, But it is failed to add review,It shows the exception as We can\'t post your review right now.
    – Jaisa
    Dec 18, 2017 at 6:54
  • 1
    this is because magento checking the title and throwing the exception, changing the definition won't make any deference. Dec 18, 2017 at 7:03
  • unable to get you, Then what should I do, Am I need to mention upgraded version??
    – Jaisa
    Dec 18, 2017 at 7:04
  • 1
    If i modified in my DB table by mannually it works fine, But need to modify definition by programmatically
    – Jaisa
    Dec 18, 2017 at 7:06
  • then try this it will change the definition. Dec 18, 2017 at 7:43
0

You can use Recurring Script which execute every time when upgrade command run:

 <?php
    
    namespace Module\Review\Setup;

    use Magento\Framework\Setup\ModuleContextInterface;
    use Magento\Framework\Setup\SchemaSetupInterface;
    use Magento\Framework\DB\Ddl\Table;
    use Magento\Framework\Setup\InstallSchemaInterface;
    
    class Recurring  implements InstallSchemaInterface
    {
    
    /**
     * Upgrades DB schema for a module
     *
     * @param SchemaSetupInterface $setup
     * @param ModuleContextInterface $context
     * @return void
     */
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;
        $installer->startSetup();
        if ($installer->getConnection()->tableColumnExists('review_detail', 'title')){
            $definition = [
                'type' => Table::TYPE_TEXT,
                'nullable' => true,
                'comment' => 'Title'
            ];
            $installer->getConnection()->modifyColumn(
                $setup->getTable('review_detail'),
                'title',
                $definition
            );
        }
    }
 }

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.