Frage

I'm trying to get a better setup working for updating a model with nested properties.

Right now in my edit view I define the textFieldTag manually to create the params struct by setting up the name as "myModel[myNestedProperty][#modelID#,#key2id#][name]"

in update action...

if I just use myModel.update(params.myModel) I can't get the update to work if there are any elements that require deletion

so I destroy all the models of the nested property that have the same id as myModel, in which case it works.

The downside is that if the update fails, the nested properties are all gone.

I've tried grabbing all of the models first before deleting them and .saveing them, but for some reason that's not working.

Looks like cfwheel is setup for this kind of relation with the checkboxes, but I need it to work with textfield and select items in my form.


Update

I realized I have another issue. Essentially I would like to expand on this to be able to use it across multiple nested properties and relationships.

the issue is in the way I setup the name especially for select dropdowns:

name="myModel[myNestedProperty][#modelID#, ][nestedID]"

the issue is that the second id cannot be declared, because it will be assigned as the id rather than using the value that I select.

To be honest, this is the one issue I've been battling with my whole time. I'm dealing with it by regenerating the models in the controller, I just forgot I haven't solved that issue yet.

Is there a way I can have these values not be used at all, and have them populated from the structure dynamically?

let's say I have (truncated) a name tcat[34,0][catID] or tcat[34,][catID], (where the catID should be the 2nd ID).

the params' tcat structure that gets generated is

[34,0]{catID = 12,14,18}

or

[34,]{catID = 12,14,18}

I'd like the params' tcat structure to have multiple structs like:

[34,12]{tID = 34; catID = 12}
[34,14]{tID = 34; catID = 14}
[34,18]{tID = 34; catID = 18}

Is there a way around this?

I'm working with a lot of composite key nested properties, and if I could have this part alone working it would make it a lot easier. I know one way is to update the name with javascript, but I think that would be the (very,very) last resort.

War es hilfreich?

Lösung

Can you give this a try?

Set up a callback in the parent model that checks to see if name is blank and flags for deletion if it's blank.

<cffunction name="init">
    ...

    <!--- This could also be `beforeValidation` if you want to make `name` required for the child model --->
    <cfset beforeSave("$provisionMyNestedProperty")>
</cffunction>

<cffunction name="$provisionMyNestedProperty">
    <cfscript>
        if (StructKeyExists(this, "myNestedProperty") && IsArray(this.myNestedProperty))
        {
            for (local.i = 1; local.i <= ArrayLen(this.myNestedProperty); local.i++)
            {
                if (!StructKeyExists(this.myNestedProperty[local.i], "name") || !Len(Trim(this.myNestedProperty[local.i].name)))
                    this.myNestedProperty[local.i]._delete = true;
            }
        }
    </cfscript>
</cffunction>

I'll keep editing my answer until we can bang out a solution. Hopefully that gives you a good start though.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top