CakePHP - 允许belongsTo外键为空值
我的数据库中有一个名为通知的表和一个名为注释的表。通知在笔记上有一个外键引用,我希望 CakePHP 认识到该值可以为 null,我是否需要在模型中进行任何设置才能使其生效?或者只需要对数据库进行约束?
I have a table called notifications in my DB and a table called notes. Notifications has a foreign key reference on notes, I want CakePHP to recognize that this value can be null, do I need to set anything up in my model for this to take effect? Or only the constraint on the DB is required?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我经常这样做。您将遇到的唯一问题是 Cake 将空值存储为空字符串,这对外键不利。我写了一个简单、愚蠢的
NullableBehavior
来解决这个问题。I do this fairly frequently. The only problem that you'll run into is that Cake stores empty values as empty strings which is bad for foreign keys. I wrote a simple, stupid
NullableBehavior
to fix that.