rose foos l7ktv3fetcs unsplash 2

Let we have two related SQL tables:

CREATE TABLE `user` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `login` varchar(255) COLLATE utf8_bin NOT NULL,
  `email` varchar(255) COLLATE utf8_bin NOT NULL,
  `password` varchar(255) COLLATE utf8_bin NOT NULL,
  `firstname` varchar(255) COLLATE utf8_bin NOT NULL,
  `lastname` varchar(255) COLLATE utf8_bin NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin

and

CREATE TABLE `employee` (
  `userid` int(10) unsigned NOT NULL,
  `occupation` smallint(5) unsigned NOT NULL,
  `birthdate` date NOT NULL,
  `hired` date NOT NULL,
  `fired` date DEFAULT NULL,
  `address` varchar(255) COLLATE utf8_bin NOT NULL,
  `phone` varchar(255) COLLATE utf8_bin NOT NULL,
  `tz` char(9) COLLATE utf8_bin NOT NULL,
  PRIMARY KEY (`userid`),
  KEY `occupation` (`occupation`),
  CONSTRAINT `employee_ibfk_2` FOREIGN KEY (`userid`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin

Then we want to create a model which can be used to make a form editing both tables at once.

Here is a solution for Yii framework:

Note that this solution is draft and preliminary. Feel free to post comments how to make it better.

Chat Icon