Yii: Combining two DB tables into one model

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.

Advertisements
This entry was posted in Uncategorized and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s