0

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'
        ),
    )
)

And $this->Category->validationErrors is:

array(
    'parent_id' => array(),
    'sort_order' => array()
)

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.

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);

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).

5
  • In what way is it failing with validation errors (the question shows it doesn't have any validation errors)? Commented Jul 29, 2013 at 10:35
  • When I said it's "failing with validation errors set but empty", I mean that the validationErrors array contains elements, but that these elements do not have any messages. Sorry if not clear, have edited question to clarify Commented Jul 29, 2013 at 10:48
  • An array with keys that are empty - is still empty. The tree behavior doesn't have any validation rules nor validation callbacks, neither does the translate behavior afaik. I recommend identifying the reason for the error (debug the save method) - or at the very least show some code that permits reproducing the problem :P. Commented Jul 29, 2013 at 11:00
  • Thanks - I'm not sure how you mean to debug the save method - debugging my call to Category::saveAll just shows "false". Any clues as to how I can find the cause of the error? There are no other errors, warning etc. My understanding is that it could only be validation or a beforeSave callback stopping the save? Commented Jul 29, 2013 at 11:23
  • saveAll is a function treat it like any method that's doing something unexpected - and debug it (put debug($foo); statements in it - determine how/why it returns false). e.g. which line in saveAll actually returns? for the method that is returning the result - which line in that function return false? etc. Please don't just answer my rhetorical questions - debug and find out. good luck. Commented Jul 29, 2013 at 12:54

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Maybe it doesn't like the null and zero value of parent_id and sort_order? Also in the database what are their field types set as? Do they allow null values? etc. I'm guessing that as there are no validation rules in the model or parent/App model, then it must be some default validation with cake's lib linking to the database/mysql table itself. So I would check the Categories table structure for the parent_id and sort_order fields.

3 Comments

some default validation with cake's lib - that doesn't exist
The fields do allow null values and are INTs.
Are you remove fields from tables which you want to translate? for example, seems you should remove name field from categories table? also, I assume, that you initialise i18n tables, by cake shell ./cake i18n

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.