1

I'm writing a CRUD system using CodeIgniter, implementing some 'one to many' dependencies between models (without using any ORM or DataMapper systems) by calling methods from one model to another.

For example: in a "many Documents per User" realeation, when deleting a User, the User_Model directly calls the delete() method on the Document_Model, deleting any documents associated with it.

I’m certain there’s a better way implementing 'one to many' model releation (without ORM, etc.. ), and would appreciate some guidance.

Thanks

Alon.

tereško
  • 58,060
  • 25
  • 98
  • 150
Alon
  • 83
  • 1
  • 4

1 Answers1

0

Of course! You are allowed to use InnoDB engine. This engine has functionally foreign keys, so you can do smth like that, for example (very basic)

CREATE TABLE IF NOT EXISTS `documents` (
  `did` bigint(11) NOT NULL AUTO_INCREMENT,
  `uid` bigint(11) NOT NULL,
  `data` varchar(255) NOT NULL,
  PRIMARY KEY (`did`),
  KEY `uid` (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `user` (
  `uid` bigint(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(25) NOT NULL,
  `password` varchar(25) NOT NULL,
  PRIMARY KEY (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
ALTER TABLE `documents`
ADD CONSTRAINT `documents_cstr_1` FOREIGN KEY (`uid`) REFERENCES `user` (`uid`) ON DELETE NO ACTION ON UPDATE CASCADE;
sly
  • 149
  • 1
  • 6