duanlinjian5819
2014-01-15 10:58
浏览 68
已采纳

cakephp:更新hasmany相关表

For example, if we have:

Array
(
    [User] => Array
        (
            [id] => 121
            [name] => Gwoo the Kungwoo
            [created] => 2007-05-01 10:31:01
        )
    [Comment] => Array
        (
            [0] => Array
                (
                    [id] => 123
                    [user_id] => 121
                    [title] => On Gwoo the Kungwoo
                    [body] => The Kungwooness is not so Gwooish
                    [created] => 2006-05-01 10:31:01
                )
            [1] => Array
                (
                    [id] => 124
                    [user_id] => 121
                    [title] => More on Gwoo
                    [body] => But what of the 'Nut?
                    [created] => 2006-05-01 10:41:01
                )
        )
)

from http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html

How should we do an update on the Comment table from the User controller given that all these data is submitted using one form?

This is somehow the same as my question here: updating related tables in cake which I unfortunately got no correct answer. Can someone enlighten me on how this works?

图片转代码服务由CSDN问答提供 功能建议

例如,如果我们有:

  Array 
  (
 [User] => Array 
(
 [id] => 121 
 [name] => Gwoo the Kungwoo 
 [created] => 2007-05-01 10:31:  01 
)
 [注释] =>数组
(
 [0] =>数组
(
 [id] => 123 
 [user_id] => 121 
 [标题]  ] =>在Gwoo上Kungwoo 
 [body] => Kungwooness不是那么Gwooish 
 [created] => 2006-05-01 10:31:01 
)
 [1] =&gt  ;数组
(
 [id] => 124 
 [user_id] => 121 
 [title] =>更多关于Gwoo 
 [body] =>但是'Nut是什么?  n [created] => 2006-05-01 10:41:01 
)
)
)
   
 
 

来自 http://book.cakephp.org/2.0/en/models/associations-linking-models-together .html

如果使用一个表单提交所有这些数据,我们应该如何从用户控制器对Comment表进行更新?

这与我的问题在某种程度上相同:更新 蛋糕中的相关表格,我很遗憾没有得到正确答案。 有人可以告诉我这是如何工作的吗?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • donglu5235 2014-01-15 11:23
    已采纳

    Use $this->User->saveAssociated($yourArray);

    it will save all the data accordingly

    已采纳该答案
    打赏 评论
  • dqvy87517 2014-01-15 12:01
    打赏 评论

相关推荐 更多相似问题