CAKEPHP 4->如何保存加入记录
我正在处理CakePHP 4应用程序,并且我已经在类别
和产品
properts categoriesproducts 之间设置了一个加入表。
当我使用名为 categories_products
的属性创建新产品时,其中包含一个类似的数组:
[
[
'category_id' => `x`
]
]
应该保存在JOIN表中的是 category_id
,新生成的 代码> product_id 。
数据未保存到联接表。
在Savemany功能中,我通过:
[
'associated' => [
'Categories'
]
]
尝试添加关联=>类别生产我会发现该关系未定义的错误。
有人可以向我解释如何填充加入表吗?我已经设置了数据库,并用CakePHP助手烘烤模型。
提前致谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
借助默认关联保存过程,您无需直接添加数据以直接添加数据,而是使用目标关联属性并提供要链接的记录的主要键。
因此,在您的情况下,可能是
类别
,例如用于修补/创建实体的数据将是这样的,其中id
是现有类别的主要键您想与您的新产品相关联:ORM将自动创建正确的加入表记录。
另请参见
With the default association saving process you do not need to add data for join tables directly, instead you use the target association property and provide the primary keys of the records that you want to link.
So in your case that would probably be
categories
, eg your data for patching/creating an entity would be structured like this, whereid
is the primary key of the existing category that you want to associate to your new product:The ORM will then create the proper join table record automatically.
See also