CakePHP - 具有预定义模型的 HABTM
我有两个模型:Bar HABTM Foo。现在我有bars_foos 连接表和一些附加字段。
当我添加带有一些 Foo 关联的新 Bar 时,反之亦然,我需要运行许多验证检查,以确定新关联是否符合要求。检查基于定义的附加字段和已保存的关联。
我应该在哪里运行这些验证?在 Bar/Foo 控制器中?或者我应该创建带有验证规则的 BarsFoo 模型?
当我保持 HABTM 关系以从 Cake 获取所有自动魔法时,我无法找到一种方法来告诉 Cake 使用我自己的预定义 BarsFoo 模型和验证规则。 Cake 创建自己的虚拟模型并忽略我的。
或者我应该(在这种特定情况下)将 HABTM 分解为 hasMany-belongsTo-hasMany 关系,在其中我可以使用自己的 BarsFoo 模型?
I have two models: Bar HABTM Foo. Now I have bars_foos join table with some additional fields.
When I add new Bar with some Foo associations or vice versa, I need to run many validation checks, whether the new associations are kosher. Checks are based on the additional fields defined and already saved associations.
Where should I run these validations? In Bar/Foo controller? Or should I create BarsFoo model with validation rules?
When I keep HABTM relationship to get all the auto-magic from Cake, I cannot find a way to tell Cake to use my own predefined BarsFoo model with validations rules. Cake creates its own virtual model and ignores mine.
Or should I (in this specific case) break HABTM into hasMany-belongsTo-hasMany relationship, where I can use my own BarsFoo model?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
with
键定义连接模型并在其中创建验证:http://book.cakephp.org/view/1044/hasAndBelongsToMany-HABTM
You can define your join model using the
with
key and create your validations there:http://book.cakephp.org/view/1044/hasAndBelongsToMany-HABTM