CakePHP保存失败,并设置了验证错误,但为空 [英] CakePHP save failing with validation errors set but empty
问题描述
我同时使用TranslateBehavior和TreeBehavior的模型无法保存。我怀疑这与这些行为之一有关。这是要输入的数据:
A model that I am using with both TranslateBehavior and TreeBehavior is failing to save. I suspect it is something to do with one of these behaviors. Here's the data going in:
array(
'Category' => array(
'id' => '3252',
'parent_id' => null,
'sort_order' => '0',
'name' => array(
'eng' => 'english name',
'fra' => 'french name',
'deu' => 'german name',
'ita' => 'italian name',
'spa' => 'spanish name'
),
)
)
和 $ this-> Category-> validationErrors
是:
array(
'parent_id' => array(),
'sort_order' => array()
)
模型中未定义验证规则。没有定义beforeSave()或beforeValidate()方法。
There are no validation rules defined in the model. There is no beforeSave() or beforeValidate() method defined.
我没有在屏幕上显示任何验证错误,尽管所有字段都有红色的错误轮廓。
I am not getting any validation errors displayed on screen, although all fields have the red "error" outline.
编辑-保存操作未达到Category :: beforeSave()的程度。我创建了该函数,但该函数无法运行。
Edit - the save operation is not getting as far as Category::beforeSave(). I created that function and it does not get run. It gets as far as Category::beforeValidate().
$validates = $this->Category->validates($this->request->data);
debug($validates);
$saved = $this->Category->saveAll($this->request->data);
debug($saved);
在上面的代码中, $ validates
为是的, $ saved
是错误的。在上述过程中的任何时候都不会调用类别beforeSave。验证似乎在调用saveAll()时失败。我需要使用saveAll而不是保存一次保存所有翻译(我在其他地方使用其他模型也没有问题)。
In the above code, $validates
is true, $saved
is false. Category beforeSave is not called at any point in the above process. The validation seems to fail on the call to saveAll(). I need to use saveAll rather than save to save all translations at once (I am doing this elsewhere with another model with no problems).
推荐答案
因此,经过一段时间的调试后,我发现了问题所在:
So, after a while debugging I have found the problem:
public $hasMany = array(
'Category' => array('className' => 'Category', 'foreignKey' => 'parent_id', 'counterCache' => true),
...
我不知道为什么写这个-我应该知道这会引起问题,我想我打算写...
I have no idea why I wrote this - I should have been aware that it was going to cause problems, I think I meant to write...
public $hasMany = array(
'Children' => array('className' => 'Category', 'foreignKey' => 'parent_id', 'counterCache' => true),
...
无论如何,将其更改为后者,这些错误已消失。
Anyway, changed it to the latter and these errors have gone.
这篇关于CakePHP保存失败,并设置了验证错误,但为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!