Creating an upgrade schema script (UpgradeSchema.php)

During the first-time module install, an upgrade schema is what gets run immediately after an install schema. We define upgrade schema within the app/code/Foggyline/Office/Setup/UpgradeSchema.php file with (partial) content as follows:

namespace FoggylineOfficeSetup;

use MagentoFrameworkSetupUpgradeSchemaInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupSchemaSetupInterface;

class UpgradeSchema implements UpgradeSchemaInterface
{
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
          /* #snippet1 */
        $setup->endSetup();
    }
}

UpgradeSchema conforms to UpgradeSchemaInterface, which requires the implementation of the upgrade method that accepts two parameters of type SchemaSetupInterface and ModuleContextInterface.

This is quite similar to InstallSchemaInterface, except the method name. The update method is run when this schema gets triggered. Within this method, we would add any relevant code we might want to execute.

Going further, let's replace the /* #snippet1 */ part from the preceding code with the following code:

$employeeEntityTable = FoggylineOfficeModelEmployee::ENTITY. '_entity';
$departmentEntityTable = 'foggyline_office_department';

$setup->getConnection()
    ->addForeignKey(
        $setup->getFkName($employeeEntityTable, 'department_id', $departmentEntityTable, 'entity_id'),
        $setup->getTable($employeeEntityTable),
        'department_id',
        $setup->getTable($departmentEntityTable),
        'entity_id',
        MagentoFrameworkDBDdlTable::ACTION_CASCADE
    );

Here, we are instructing Magento to create a foreign key on the foggyline_office_employee_entity table, more precisely on its department_id column, pointing to the foggyline_office_department table and its entity_id column.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.218.215