CakePHP : form field order problem when saving related models

On Friday afternoon I spent the last hour of my day pulling my hair out. I’ve been building a CRM system for a recruitment agency. We had just had a meeting and they asked me to change one of the relationships from Client hasAndBelongsToMany Location and Client hasMany Job to Client hasMany Job and Job hasAndBelongsToMany Location.

This should have been a quick simple change, but nothing was working. Nothing was erroring either though. This morning I came in and examined the $data POST arrays.

This is what I discovered:

This array saves the data:


Array
(
    [Candidate] => Array
        (
            [skill_1] => Swiming Instructor
            [skill_2] => French
            [skill_3] => 
            [skill_4] => 
            [skill_5] => 
            [title_1] => Mrs
            [forename_1] => Celine
            [surname_1] => Rogers
            [address] => 
            [postcode] => 
            [phone] => 
            [mobile] => 
            [notes] => 
            [email] => 
            [active] => 1
            [id] => 13
        )

    [Location] => Array
        (
            [Location] => Array
                (
                    [0] => 
                    [1] => 8
                    [2] => 11
                )

        )

)

This array doesn’t save the data:


Array
(
    [Location] => Array
        (
            [Location] => Array
                (
                    [0] => 
                    [1] => 10
                    [2] => 2
                )

        )

    [Job] => Array
        (
            [client_id] => 2
            [name] => Web Designer
            [summary] => Web Designer in Taunton
            [active] => 1
            [id] => 3
        )

    

)

The difference is in the working example the ‘key’ model in the relationship is listed first and the related model data (Location) is posted afterward.

I had a quick look at the views and ended up moving a hidden field (data[Job][client_id]) with the so that part of the ‘key’ model was posted first. Sure eneough the layout of the $data POST array had changed – and guess what the data saved correctly.


Array
(
    [Job] => Array
        (
            [client_id] => 2
            [name] => Web Designer
            [summary] => Web Designer in Taunton
            [active] => 1
            [id] => 3
        )

    [Location] => Array
        (
            [Location] => Array
                (
                    [0] => 
                    [1] => 10
                    [2] => 2
                )

        )

)

Now that I’ve got it working I can see that there is a certain sense to this – I can understand why it happens, but I certainly can’t remember reading anything about this.

The moral of the story? Maybe we need to think about the logical layout of forms more instead of shunting bits about until they look good (which is what I do in a rush). This isn’t necessarily a bad thing though, one of the benefits of using frameworks is that by following a set of rules we benefit from greater consistency of method in our code and therefore produce things that are more robust and bug free.

2 thoughts to “CakePHP : form field order problem when saving related models”

Comments are closed.