Migration
Code Generation starts with database design.
DO NOT create or modify database thru tools like PHPMyadmin. Use code instead.
Best practice in Yee is not to manually create your database, but to use code to do it. This allows collaboration in large team possible where everybody may have their own version of local database in development environment.
Migration also is a key player in modular architecture, where module can be install and upgrade easily by super admin. In this case, migration code either sit in insallDb
function at module like SampleModule.php
or; sit in the upgrades
folder.
Create Table
$migration->createTable('sample', array(
'id' => 'pk',
'code' => 'string NOT NULL',
'sample_group_id' => 'integer NULL',
'sample_zone_code' => 'string NULL',
'title_en' => 'string NOT NULL',
'title_ms' => 'string NOT NULL',
'title_zh' => 'string NOT NULL',
'text_short_description_en' => 'string NOT NULL',
'text_short_description_ms' => 'string NOT NULL',
'text_short_description_zh' => 'string NOT NULL',
'html_content_en' => 'longtext NULL',
'html_content_ms' => 'longtext NULL',
'html_content_zh' => 'longtext NULL',
'image_main' => 'string NULL',
'file_backup' => 'string NULL',
'price_main' => 'decimal(10,0) NULL DEFAULT 0',
'gender' => 'string NULL',
'age' => 'integer NULL',
'csv_keyword' => 'mediumtext NULL',
'ordering' => 'double NOT NULL DEFAULT 1',
'date_posted' => 'integer NOT NULL',
'is_active' => 'boolean NOT NULL DEFAULT 1',
'is_public' => 'boolean NOT NULL DEFAULT 1',
'is_member' => 'boolean NOT NULL DEFAULT 1',
'is_admin' => 'boolean NOT NULL DEFAULT 1',
'date_added' => 'integer NOT NULL',
'date_modified' => 'integer NOT NULL',
));
Alter Table
$migration->alterColumn('sample', 'code', 'varchar(32) NOT NULL');
$migration->alterColumn('sample', 'file_backup', 'varchar(255) NULL');
$migration->alterColumn('sample', 'gender', "ENUM('male', 'female', 'secret') NULL");
$migration->alterColumn('sample', 'age', 'smallint(6) NULL');
Create Index
Two type of index here, the simple one:
$migration->createIndex('code', 'sample', 'code', true); // a unique index
$migration->createIndex('sample_group_id', 'sample', 'sample_group_id', false); // a not unique index
and the combination (combine more than 1 table fields as an unique index, mostly appear in many-to-many table):
$migration->createIndex('campus_member2organization-campus_member_id-organization_id', 'campus_member2organization', array('campus_member_id', 'organization_id'), true);
Create Foreign Key
$migration->addForeignKey('fk_sample-sample_zone_code', 'sample', 'sample_zone_code', 'sample_zone', 'code', 'CASCADE', 'CASCADE');
Execute Raw SQL
This is handy when you need to bulk update your existing data in database.
$sql = 'UPDATE `eventbrite_organization_webhook` as t LEFT JOIN `organization` as f ON t.organization_code=f.code SET t.organization_id=f.id';
Yii::app()->db->createCommand($sql)->execute();
Create Meta
MetaStructure::initMeta('organization', 'community', 'urlYoutubeCoverVideo', 'string', 'Cover Youtube Video URL', 'URL to display youtube video in community organization page', '');
Create Setting
Setting::setSetting('sample-var1', 'Hello World 0.2', 'string');
Create Embed
$embed = Embed::setEmbed('ntis-signup-tncContent', array(
'is_title_enabled' => true,
'is_text_description_enabled' => false,
'is_html_content_enabled' => true,
'is_image_main_enabled' => false,
'is_default' => true,
'title_en' => 'Terms and Conditions',
'html_content_en' => 'Hello Admin, please remember to update this terms and condition content inside Embed \ <b>#signup-tncContent</b>',
));
Create Service
Service::setService('cv', 'CV Portfolio', 'A talent directory showcasing experience and qualifications for job opportunity and cofounder matching', array('is_bookmarkable' => 1, 'is_active' => 1));
Create Access Role
Access::setAccessRole('ntis', 'BackendController', ['manageSolutionProvider'], ['ntisTechSecretariat', 'ntisRegulatorySecretariat']);
Last updated
Was this helpful?