在添加视图中强制 belongsTo

Forcing belongsTo in add view

我有 3 个表,用户、代理和所有者。代理和所有者将是一种用户,在我的模型中,我已经在代理和所有者中正确定义了 $belongsTo()。

一切正常,因为我可以添加一个用户,然后转到代理或所有者路径并创建一个引用该用户记录的新代理或所有者。

我的问题是,允许添加新代理或所有者并自动创建和链接用户的官方蛋糕方法是什么?我想跳过创建用户的步骤,而只是创建代理或所有者并自动创建用户。

我已经阅读了此处的关系 http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html 并进行了一些搜索,但我找不到任何似乎可以描述情况的内容。

本能地,我认为这样做的方法是在 Agents 和 Owners 控制器的 add 方法中提供一些逻辑,该方法将创建一个新用户,然后以下拉列表的方式修改视图用户没有显示,但是在 cake 中有更正确的方法吗?

My question is, what is the official cake way to allow a new agent or owner to be added and a user is automatically created and linked up? I would like to skip the step of creating a user, and instead just create an agent or an owner and have the user created automatically.

只需使用Model::afterSave()。检查它是否是一条新记录,然后创建关联的记录。