CodeIgniter/Datamapper - 更新一对多关系
与 CodeIgniter/Datamapper 更新一对多关系时遇到问题。在我的脑海中,我有这样一个想法:一对多关系何时更新。关系表中的现有记录将被删除,并添加新的关系。事实似乎并非如此。
使用以下内容,每次都会向关系表添加新记录:
$item->save($banners);
创建的关系是正确的,但我期望关系表中仅包含 $banners
中包含的对象,而不是任何历史项。
这是正确的功能吗?如果是,使此更新过程正常工作的最佳方法是什么?
谢谢
Having an issue with update one-to-many relationships with CodeIgniter/Datamapper. In my mind I had the notion that when a one-to-many relationship was updated. The existing records within the relationship table would be removed and the new relationships added. This appears not to be the case.
Using the following, adds new records to the relationship table each time:
$item->save($banners);
The relationships that are created are correct but I was expecting only the objects contained within $banners
to be included in the relationship table not any historial items.
Is this correct functionality and if so what is the best method for making this update process work?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一对多并不意味着“只有一个可以关联”。请参阅 维基百科
它只是意味着关系的“一”方包含外部钥匙。
如果您想要此行为,您需要手动重置现有关系。使用更新查询速度最快,其中将“foreign_key”设置为 NULL,其中“foreign_key”是要关联的对象的“id”值。
One-to-many doesn't mean "only one can be related". See Wikipedia
It just means that the "one" side of the relationship contains the foreign key.
You need to manually reset existing relations if you want this behaviour. This goes quickest using an update query where you set 'foreign_key' to NULL where 'foreign_key' is the 'id' value of the object to be related.