Extra Value in array when editing a value in ManyToMany

0 views
Skip to first unread message

Devon Burriss

unread,
May 27, 2009, 4:05:47 AM5/27/09
to transfer-dev
As an example I have a many to many relationship between Product and
ProductCategory.
When I edit a product, it updates the DB but I have a duplicate in my
Product array. This duplicate is not in the DB, just the array.

Any ideas why this would happen?

Thanks
Devon

Mark Mandel

unread,
May 27, 2009, 4:12:46 AM5/27/09
to transf...@googlegroups.com
Are you adding more than one?

Mark
--
E: mark....@gmail.com
W: www.compoundtheory.com

Devon Burriss

unread,
May 27, 2009, 4:46:44 AM5/27/09
to transfer-dev
Not sure I understand the question correctly.
I am only adding 1 at a time if that is the question. This problem is
occurring when I edit an existing Product.
I came up with a solution but the side effect (I think but have not
tested) will be that I cannot edit the Product so it is in another
category using this function. Although I'm not I could do that
anyway :)
I solved the problem as such:

<cfif IsInstanceOf(this,"components.IMultiRelational") AND NOT
instance.object.getIsPersisted() AND NOT instance.object.getIsDirty()>
<cfset instance.object = setRelationships
(instance.object,arguments.form)>
</cfif>
=============
<cffunction name="setRelationships" access="public"
returntype="Struct">
<cfargument name="object" type="Struct" required="true">
<cfargument name="form" type="Struct" required="true">

<cfset shelf = transfer.get("basic.Shelf",arguments.form.shelfidfk)>
<cfset shelf.addProduct(object)>
<cfset transfer.save(shelf)>

<cfreturn ARGUMENTS.object>
</cffunction>
> E: mark.man...@gmail.com
> W:www.compoundtheory.com

Devon Burriss

unread,
May 27, 2009, 5:04:05 AM5/27/09
to transfer-dev
Ok. That didn't actually work since it also stops me from adding new
ones...

Matt Quackenbush

unread,
May 27, 2009, 5:26:33 AM5/27/09
to transf...@googlegroups.com
I am going to take a wild guess and say that when you are editing, you are not using clone() on whatever object(s) you are editing, and as a result a duplicate is being added to the cache.  Another possible solution is to use an AfterUpdate observer to reset the m2m after an edit.

Devon Burriss

unread,
May 27, 2009, 5:43:07 AM5/27/09
to transfer-dev
Ok. So now I can add and edit without error or duplicates. Solution
was...
If it breaks something else i'll post it but so far this worked for
me. Please not that I am saving the product with transfer and the call
the function below, passing through the saved transfer object as
object and form has a hidden field in it which contains the id of the
productcategory, or shelf as i call it.

<cffunction name="setRelationships" access="public"
returntype="Struct">
<cfargument name="object" type="Struct" required="true">
<cfargument name="form" type="Struct" required="true">

<cfset shelf = transfer.get("basic.Shelf",ARGUMENTS.form.shelfidfk)>
<cfif NOT shelf.containsProduct(ARGUMENTS.object)>
<cfset shelf.addProduct(ARGUMENTS.object)>
<cfset transfer.save(shelf)>
</cfif>

<cfreturn ARGUMENTS.object>
</cffunction>

On May 27, 10:46 am, Devon Burriss <de...@nervstudios.co.za> wrote:

Mark Mandel

unread,
May 27, 2009, 6:49:44 AM5/27/09
to transf...@googlegroups.com
I tend to clear() then re- add(), that works for me. Depends on your process.

Mark
Reply all
Reply to author
Forward
0 new messages