【问题标题】:CakePhp saveAll messes up data in associated modelCakePhp saveAll 弄乱了关联模型中的数据
【发布时间】:2011-07-27 15:50:20
【问题描述】:

故事:一个表单获取两个模型(Project 和关联的 ProjectDetail)的输入,控制器尝试使用 saveAll 保存表单数据。相关模型的某些字段的验证失败,即使它不应该(例如,1234 未能通过规则“数字”)。经过一番折腾,我把 debug($this->data);在每个模型的 beforeValidate() 调用中调用,请注意“成本”和“收入”字段如何以某种方式丢失其前导数字(而日期字段没有。该字段也可以正确验证)。

Project -> beforeValidate()

$this->data = Array
(
    [Project] => Array
        (
            [project_type_id] => 1
            [name] => asdf
            [cost_center] => 1234
        )

    [ProjectDetail] => Array
        (
            [cost] => 1234
            [revenue] => 1234
            [project_start] => 2011-07-27
        )

)

ProjectDetail -> beforeValidate()

$this->data = Array
(
    [ProjectDetail] => Array
        (
            [cost] => 234
            [revenue] => 234
            [project_start] => 2011-07-27
        )

)

虽然这本身很烦人,但似乎无法解释验证失败的原因,因为这两个字段看起来仍然像数字。所以我也在 beforeValidate 方法中运行了以下内容:

    $cost = $this->data['ProjectDetail']['cost'];
    debug('#'.$cost.'#'); //Check for obscure non-printables
    debug(is_string($cost));
    debug(ctype_digit($cost));

还有输出:

Project -> beforeValidate()
#1234#
true
true


ProjectDetail -> beforeValidate()
#234#
true
false

所以,不知何故,这个字符串连同它的前导数字一起失去了它的数字性。奇怪的。任何想法表示赞赏。

编辑:是的,模型可以自行保存。

V.S. PHP 5.3.6 蛋糕 1.3.10 & 1.3.11

【问题讨论】:

  • 你的 beforeValidate 代码是什么?
  • 只有几个 debug();上述调用
  • 我认为这是一个错误,我正要发布同样的内容!奇怪的是,我的琴弦前面有一个“1”。例如 'test' 获取保存为 '1test'

标签: php cakephp


【解决方案1】:

记得在 beforeValidate 中return true;

我不清楚您遇到的前导数字问题。但是对于数字性:您不能在 beforeValidate 中访问$this->data['ProjectDetail']['cost'],因为 $this 将引用模型,而不是控制器。我不知道为什么它没有给你一个错误。

【讨论】:

  • 哦,这就是你的意思......是的,我回来了true
猜你喜欢
  • 1970-01-01
  • 2014-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-24
  • 1970-01-01
  • 2013-09-07
相关资源
最近更新 更多