I have 2 tables and I want to create association between them, however bin/cake doesn't do it correctly.
CREATE TABLE `teams` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`joiningID` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
`name` varchar(45) COLLATE utf8_unicode_ci NOT NULL,
`admin` int(11) NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `admin` (`admin`),
UNIQUE KEY `name` (`name`),
CONSTRAINT `fk_admin` FOREIGN KEY (`admin`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=34 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
USERS:
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`email` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`encrypted_password` varchar(80) COLLATE utf8_unicode_ci NOT NULL,
`salt` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
`facebook_json` varchar(700) COLLATE utf8_unicode_ci DEFAULT NULL,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
`team` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`),
KEY `users_ibfk_1` (`team`),
CONSTRAINT `users_ibfk_1` FOREIGN KEY (`team`) REFERENCES `teams` (`id`) ON DELETE SET NULL ON UPDATE SET NULL
) ENGINE=InnoDB AUTO_INCREMENT=168 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
Whenever I do bin/cake bake it creates all classes but it doesn't create associations. Here is my part of my Team Entity and it has int $admin and not User Entity...
* @property int $id
* @property string $joiningID
* @property string $name
* @property int $admin
* @property \Cake\I18n\FrozenTime $created
* @property \Cake\I18n\FrozenTime $modified
I assume that you want to use bin\cake bake
to generate model, views and controllers.
CakePHP assumes that the foreign keys follows the following convention : <singularized version of the table name>_id
. So in your case you should rename your team
foreign key to team_id
, and the relationships should be correctly detected.
So your user
entity would have a belongsTo relationship with the teams
table. CakePHP would then create a team
variable in your entity to link to the associated team
entity.