3

I knew how to add attributes for product entity:

$eavSetup->addAttribute(
                \Magento\Catalog\Model\Product::ENTITY,
                'period_date_start',/* Custom Attribute Code */
                [
                        'group' => 'Period Date',/* Group name in which you want to display your custom attribute */
                        'type' => 'datetime',/* Data type in which formate your value save in database*/
                        'frontend' => 'Magento\Eav\Model\Entity\Attribute\Frontend\Datetime',
                        'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\Datetime',
                        'label' => 'Start Date', /* lablel of your attribute*/
                        'input' => 'date',
                        'class' => '',
                        'source' => '', /* Source of your select type custom attribute options*/
                        'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE, /*Scope of your attribute */
                        'visible' => true,
                        'required' => false,
                        'user_defined' => true,
                        'default' => '',
                        'searchable' => false,
                        'filterable' => false,
                        'comparable' => false,
                        'visible_on_front' => false,
                        'used_in_product_listing' => true,
                        'unique' => false,
                        'sort_order' => 1
                ]
        );

But how to add Attribute for cms_page ?

2

I found solution below, it can solve my problem

If you want to add more column into existing table of your module you could do following.

Step 1: Create UpgradeSchema.php under Setup folder. Get Idea from following code.

namespace Vendor\ModuleName\Setup;

use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
class UpgradeSchema implements  UpgradeSchemaInterface
{
    public function upgrade(SchemaSetupInterface $setup,
                            ModuleContextInterface $context){
        $setup->startSetup();
        if (version_compare($context->getVersion(), '1.0.1') < 0) {

            // Get module table
            $tableName = $setup->getTable('table_name');

            // Check if the table already exists
            if ($setup->getConnection()->isTableExists($tableName) == true) {
                // Declare data
                $columns = [
                    'imagename' => [
                        'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                        'nullable' => false,
                        'comment' => 'image name',
                    ],
                ];

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

            }
        }

        $setup->endSetup();
    }
}

Step 2: Change the setup_version value in module.xml

Step 3: Run php bin/magento setup:upgrade command from CLI

link: Magento2 : How to database schema upgrade

| improve this answer | |
  • Can you please explain this more clear. Because I have created the UpgradeSchema.php in my module under setup folder and I executed the command wahat you have mentioned above. But No custom attributes reflected in CMS page. Kindly help me. – Deeban Babu Sep 19 '16 at 11:26
  • The install scripts that I have seen have a command like $installer->getConnection()->createTable($table) after all of the definitions. Perhaps your data is correct but it isn't fully processing or attempting a save? I'm not sure if that createTable or a similar updateTable is necessary. – LM_Fielding Oct 17 '16 at 9:50
  • To be honest, this isn't the correct answer as the solution is impossible as worded in the question - I had the same problem as you. – LM_Fielding Jun 15 '17 at 13:51
0

I checked the source for the CMS module, and the way they're adding the attributes is good enough for me. I would prefer a setup object like category or product, but I see nothing wrong with this.

     /**
     * Create table 'cms_page'
     */
    $table = $installer->getConnection()->newTable(
        $installer->getTable('cms_page')
    )->addColumn(
        'page_id',
        \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
        null,
        ['identity' => true, 'nullable' => false, 'primary' => true],
        'Page ID'
    );

    $installer->getConnection()->createTable($table);

Reference: https://github.com/magento/magento2/blob/develop/app/code/Magento/Cms/Setup/InstallSchema.php

| improve this answer | |
  • It is impossible to add EAV attributes to Cms/Page model - this is currently the only option (2.1.4) – LM_Fielding Jun 15 '17 at 13:50

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.