Changing the period field to a one-to-many
Posted by mholmes on 10 Feb 2011 in Activity log
Changed the existing doc_to_period_id relationship into a one-to-many field. These were the steps:
- Create a docs_to_docPeriods table:
-- -- Table structure for table `docs_to_docPeriods` -- CREATE TABLE IF NOT EXISTS `docs_to_docPeriods` ( `dtp_dtp_id` int(11) NOT NULL auto_increment, `dtp_doc_id_fk` int(11) NOT NULL, `dtp_docPeriod_id_fk` int(11) NOT NULL, PRIMARY KEY (`dtp_dtp_id`), KEY `dtp_docPeriod_id_fk_idx` (`dtp_docPeriod_id_fk`), KEY `dtp_doc_id_fk_idx` (`dtp_doc_id_fk`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
- Add constraints to it:
-- -- Constraints for table `docs_to_docPeriods` -- ALTER TABLE `docs_to_docPeriods` ADD CONSTRAINT `docs_to_docPeriods_ibfk_1` FOREIGN KEY (`dtp_doc_id_fk`) REFERENCES `documents` (`doc_id`) ON DELETE CASCADE ON UPDATE CASCADE, ADD CONSTRAINT `docs_to_docPeriods_ibfk_2` FOREIGN KEY (`dtp_docPeriod_id_fk`) REFERENCES `docPeriods` (`dp_id`) ON DELETE CASCADE ON UPDATE CASCADE;
- Copy the existing data over to it:
INSERT INTO `docs_to_docPeriods` (`dtp_doc_id_fk`, `dtp_docPeriod_id_fk`) SELECT `doc_id`, `doc_to_docPeriods_id` FROM `documents`
- Add the support for this in
local_classes.php
:$this->addField(new MdhOneToManyField('doc_periods', 'Period(s)', 'doc_id', 'disPeriods', 'dp_id', 'dp_name', 'docs_to_docPeriods', 'dtp_dtp_id', 'dtp_doc_id_fk', 'dtp_docPeriod_id_fk', true, 'dp_name', true));
- Comment out the original field (we'll leave the data in there for a while, just in case).
- Test on dev db, then migrate to live.