bin / cake不创建关联

时间:2018-10-02 12:35:32

标签: cakephp cakephp-3.0 cakephp-bake

我有2个表,我想在它们之间创建关联,但是bin / cake不能正确执行。

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

用户:

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

每当我进行bin / cake烘烤时,它都会创建所有类,但不会创建关联。这是我的团队实体的一部分,它具有int $ admin而不是User Entity ...

 * @property int $id
 * @property string $joiningID
 * @property string $name
 * @property int $admin
 * @property \Cake\I18n\FrozenTime $created
 * @property \Cake\I18n\FrozenTime $modified

1 个答案:

答案 0 :(得分:0)

我假设您想使用bin\cake bake来生成模型,视图和控制器。

CakePHP假定外键遵循以下约定:<singularized version of the table name>_id。因此,在您的情况下,应将team外键重命名为team_id,并应正确检测到这些关系。

因此,您的user实体将与teams表具有belongsTo关系。然后,CakePHP将在您的实体中创建一个team变量以链接到关联的team实体。

请参阅:https://book.cakephp.org/3.0/en/orm/associations.html