CakePHP 更新多对多资源
我有类别产品多对多关系。创建产品时,会选择一个或多个类别,然后保存产品。这工作正常并且连接表已正确填充。问题是当我去编辑产品时,我可以添加更多类别,更改它们等......当我尝试保存时,问题出现了,它失败了。
我用来保存的行是:
$this->Product->saveAll($this->data)
$this->Product->id 已正确填充,并且 $this->data 的调试给了我一个像这样的数组:
Array(
[Product] => Array
(
[0] => 17
)
[Category] => Array
(
[0] => Array
(
[0] => 85
)
[1] => Array
(
[0] => 96
)
)
)
我不知道为什么它没有t 保存,因为我在任何地方都找不到有关错误的任何详细信息。
非常感谢任何帮助。
I have a category-product many to many relationship. When a product is created one or more categories are selected and then the product is saved. This works fine and the join table is correctly populated. The issue is when I go to edit the product, I can add more categories, change them etc... The issue comes when I try to save, it fails.
The line that I'm using to save is:
$this->Product->saveAll($this->data)
$this->Product->id is correctly populated and the debug of $this->data gives me an array like so :
Array(
[Product] => Array
(
[0] => 17
)
[Category] => Array
(
[0] => Array
(
[0] => 85
)
[1] => Array
(
[0] => 96
)
)
)
I don't know why it doesn't save as I can't find any detail on the error anywhere.
Any help much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这个结构是错误的。如果您正在编辑,产品的“id”字段在哪里?
我在应用程序中这样做。我明天会检查一下(今天我无法查看源代码),但我认为 id 是问题所在。
I think is this structure is wrong. If you are editing, where's the 'id' field of the product?
I do that in an application. I will check it out tomorrow(today I cant view the source), but i think the id is the problem.
正如 raultm 所说,结构略有错误,但结果在模型中得到了验证。名称和描述字段设置为不允许为空,并且在结构中我没有传递这些字段(不知道您必须进行更新)。通过添加缺少的字段并在数组中正确命名字段(即 id、名称和描述),此方法有效。
As raultm says, the structure was slightly wrong but it turned out to be validation in the model. The name and description fields were set to not allow empty and in the structure I wasn't passing these in (wasn't aware you had to on an update). By adding in the missing fields and naming the fields correctly in the array i.e. id, name and description, this worked.