Story:表单获取两个模型(Project和关联的ProjectDetail)的输入,控制器尝试使用saveAll保存表单数据。关联模型的某些字段的验证失败,即使它不应该失败(例如1234失败规则"数字")。经过一番折腾,我把debug($this->data);在每个模型的beforeValidate()调用中调用,请注意'cost'和'revenue'字段如何以某种方式丢失其前导数字(而date字段没有)。该字段也可以正确验证)。
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
因此,不知何故,这个字符串连同它的前导数字一起失去了它的数字性。奇怪。谢谢。
编辑:是的,模型本身保存得很好。糖丸Php 5.3.6 Cake 1.3.10 &1.3.11
在beforeValidate中记住return true;
我不清楚你遇到的前导数字问题。但是对于数值性:您不能在beforeValidate中访问$this->data['ProjectDetail']['cost']
,因为$this将引用模型,而不是控制器。我不知道为什么它没有给你一个错误