CakePhp saveAll messes up data in associated model
Story: a form gets input for two models (Project and associated ProjectDetail), controller tries to save form data with saveAll. Validation fails for some fields of the associated Model, even though it shouldn't (e.g. 1234 fails rule 'numeric'). After some screwing around I put debug($this->data); calls in the beforeValidate() calls of each Model, note how the 'cost' and 'revenue' fields each somehow lose their leading digit (while the date field doesn't. This field also validates properly).
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
)
)
While this is annoying in itself, it doesn't seem to explain why the validation fails, since the two fields still look like numbers. So I ran the following in the beforeValidate methods too:
$cost = $this->data['ProjectDetail']['cost'];
debug('#'.$cost.'#'); //Check for obscure non-printables
debug(is_string($cost));
debug(ctype_digit($cost));
And the output:
Project -> beforeValidate()
#1234#
true
true
ProjectDetail -> beforeValidate()
#234#
true
false
So, somehow, this string lost its numeric-ness along with its leading digit. Strange. Any thoughts appreciated.
Edit: Yes, the Model saves just fine on its own.
V.S. Php 5.3.6 Cake 1.3.10 & 1.3.11
remember to return true;
in beforeValidate.
I'm unclear about the leading digit problem you're having. But for the numeric-ness: you can't access $this->data['ProjectDetail']['cost']
in beforeValidate, because $this would refer to the model, not the controller. I'm not sure why it didn't throw you an error.
精彩评论