CF 11 ORM one-to-many entitySave issue

80 views
Skip to first unread message

Justin

unread,
Jun 14, 2015, 5:28:46 AM6/14/15
to cf-or...@googlegroups.com
Hi,

Tried to post it before but for some reason it didn't go through, so here it is again:
I can't figure out how to set up the properties to be able to save a profile and all the plans associated with the plan.
Hoping somebody can shed some light.
I have the following (simplified) database structure:

profiles table
profileID
title

plans table
planID
name

profilesPlans table
profileID
planID
otherInfo

//Profile.cfc
component output="false" persistent="true" table="profiles"
{

property name="profileID" type="numeric" unSavedValue="0" default="0" fieldtype="id" column="profileID" unique="true" notnull="true" generated="insert" generator="identity";
property name="title" type="string" max="255" default="" required="true" column="title";
property name="plans" fieldtype="one-to-many" cfc="model.ProfilePlan" fkcolumn="profileID" type="array" singularname="plan" cascade="all" invers="true";


public function init () output="false" {
if (structKeyExists (arguments, "profileID") && arguments.profileID > 0)
return entityLoadByPK ("profile", arguments.profileID);
else
return this;
}

}

//Plan.cfc
component extends="Base" persistent="true" output="false" table="plans"
{
property name="planID" type="int" unSavedValue="0" fieldtype="id" unique="true" notnull="true" generator="native" setter="false" sqltype="int";
property name="name" type="string" length="50" required="true" notnull="true" sqltype="varchar(50)" ;

public function init () output="false" {
if (structKeyExists (arguments, "planID") && arguments.planID > 0)
return EntityLoad ('planID', arguments.planID, true);
else
return this;
}

}

//ProfilePlan.cfc
component persistent="true" output="false" table="profilesPlans"
{
property name="profileID" fieldtype="id,many-to-one" fkcolumn="profileID" cfc="model.Profile";
property name="planID" fieldtype="id,many-to-one" fkcolumn="planID" cfc="model.Plan";
property name="otherName" type="string" length="255" sqltype="varchar(255)" ;
}

When trying to save a profile:

var profile = entityNew ("Profile");

// set plans
var PlansObj = [];
for (var id in stArgs.plans )
{
var profilePlan = entityNew ("ProfilePlan");
profilePlan.setProfileID (profile.getProfileID());
profilePlan.setPlanID (id);

if (structKeyExists (arguments, "otherName" & id))
profilePlanType.setOtherName (arguments["otherName" & id]);
arrayAppend (PlansObj, profilePlan);
}
profile.setPlans (PlansObj);

entitySave (profile);

This is the error:
You have attempted to dereference a scalar variable of type class java.lang.String as a structure with members.

I have tried a few variances of the above, but no luck.

Thanks
Justin

Cameron Childress

unread,
Jun 15, 2015, 9:05:23 AM6/15/15
to cf-or...@googlegroups.com
You didn't indicate what line the error occurred on but I'd put a bet that stArgs.plans is a string, or that stArgs is a string. If you are still stuck on this you might consider putting this code in a gist to make it more readable and posting more of the error including the line numbers. you can then just drop the gist link into your posting.

-Cameron

Justin

--
You received this message because you are subscribed to the Google Groups "cf-orm-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cf-orm-dev+...@googlegroups.com.
To post to this group, send email to cf-or...@googlegroups.com.
Visit this group at http://groups.google.com/group/cf-orm-dev.
For more options, visit https://groups.google.com/d/optout.



--
Cameron Childress
--
p:   678.637.5072
im: cameroncf

Justin

unread,
Jun 15, 2015, 9:35:39 AM6/15/15
to cf-or...@googlegroups.com
Hi,

Sorry, the stArgs.plans is actually and array:

stArgs.plans = listToArray (arguments.plans);
where arguments.plans is the list of plans to be associated with plan.

The error is happening on entitySave (profile) line

The way I see it, the mapping is not done properly.
I would have expected that by setting the one-to-many relationship, hibernate would know to manage the saving without having to set:
profilePlan.setProfileID (profile.getProfileID());
Because when I dump the profile (before EntytiSave) I see the plans array but the profileID is 0 (or empty string if I don't set it up).
I thought that hibernate will save first the profile and then say hey I have this relationship that I have to save and I need to update the profileID with the id that I just got after saving the profile.

Hopefully I make sense.

Thanks
Justin


On Monday, June 15, 2015 at 9:05:23 AM UTC-4, Cameron Childress wrote:
> You didn't indicate what line the error occurred on but I'd put a bet that stArgs.plans is a string, or that stArgs is a string. If you are still stuck on this you might consider putting this code in a gist to make it more readable and posting more of the error including the line numbers. you can then just drop the gist link into your posting.
>
>
> -Cameron
>
>
>
>

Justin

unread,
Jun 16, 2015, 9:22:05 AM6/16/15
to cf-or...@googlegroups.com
I was bale to fix it by changing:

//ProfilePlan.cfc
component persistent="true" output="false" table="profilesPlans"
{
property name="profileID" fieldtype="id,many-to-one" fkcolumn="profileID" cfc="model.Profile";
property name="planID" fieldtype="id,many-to-one" fkcolumn="planID" cfc="model.Plan";
property name="otherName" type="string" length="255" sqltype="varchar(255)" ;
}

to this:


//ProfilePlan.cfc
component persistent="true" output="false" table="profilesPlans"
{
property name="profileID" fieldtype="id,many-to-one" fkcolumn="profileID" cfc="model.Profile";

property name="planID" fieldtype="id";


property name="otherName" type="string" length="255" sqltype="varchar(255)" ;
}

Thanks

Justin

unread,
Jun 23, 2015, 3:46:29 PM6/23/15
to cf-or...@googlegroups.com

I can't get any love from ORM.

I have updated my components as show bellow.
As a result, I can properly Save/Delete a profile and all the ProfilePlan dependencies are properly saved/ deleted.
The issue I have is when trying to update a profile.
For example if I add a plan then the save properly adds the new plan (to profilesPlans), but if I delete a plan, it doesn't delete it from the profilesPlans table.
I was under the impression that ORM is smart enough to detect the deletes. I hope that it's just a miss on my end. I'm sure that I'm not the only one doing this.

Thank you


//Profile.cfc
component output="false" persistent="true" table="profiles"
{

property name="profileID" type="numeric" generator="identity";
property name="title" type="string" column="title";
property name="plans" fieldtype="one-to-many" cfc="ProfilePlan" fkcolumn="profileID" structKeyColumn="planID" type="struct" singularname="plan" cascade="all-delete-orphan" inverse="true";

}

}

//Plan.cfc
component extends="Base" persistent="true" output="false" table="plans"
{
property name="planID" type="int" fieldtype="id" generator="native";
property name="name" type="string" ;
}
}

//ProfilePlan.cfc
component persistent="true" output="false" table="profilesPlans"
{

property name="profile" fieldtype="id,many-to-one" fkcolumn="profileID" cfc="Profile" notnull="true" ;
property name="plan" fieldtype="id,many-to-one" fkcolumn="planID" cfc="Plan" invers="true";

Message has been deleted

Justin

unread,
Nov 4, 2015, 10:22:33 PM11/4/15
to cf-or...@googlegroups.com
Hello,
Sorry for the late reply. Yes, I have tried all the options with no luck. 
I can't believe that I'm the only one having this issue?

ORM sometimes is so nice and sometimes makes me go nuts and very hard to find answers.

Thanks
Justin 

On Fri, Sep 4, 2015 at 10:14 AM, Drew Dulgar <drewb...@gmail.com> wrote:
Have you tried utilizing the property cascade property directives?

On relationships, cascade can be set to one of the following:

all: Allows you to apply all operations to be cascaded to the associated object.
save-update: If the parent object is saved, the associated objects are saved as well.
delete: Deletes the child object if the delete operation is called on the parent object.
delete-orphan: This is a special kind of cascade option that applies to one-to-many relation only. Deletes all child objects for which the association has been removed.
all-delete-orphan: Cascades all operations to child objects, and performs delete-orphan action.
refresh: Cascades the refresh action to the child object. The refresh action is used to reload an object and its collections.
Typically, cascade attribute is not used on a many-to-one or a many-to-many relationship.

http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WS5FFD2854-7F18-43ea-B383-161E007CE0D1.html#WS7CF54B7A-8F03-47bf-98B5-56FD8B8C858A

--
You received this message because you are subscribed to a topic in the Google Groups "cf-orm-dev" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cf-orm-dev/Gin-Y7FNnbk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cf-orm-dev+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages