Thanks for the reply Andrew, I'm not sure I follow where your going with your suggestion of :" x = EntityLoadByExample(x) if(isNUll(x)) x.save()"
"x.save()" would throw in error in the case that x is null. In the majority of cases where this check is needed, it's a case of "create if needed, retrieve the instance either way" - so with a try/catch I would still need to retrieve the existing record. However, even in cases where I want to enforce uniqueness, relying only on a try-catch here requires that the unique constraint *actually* exist in the database (which it certainly should - but even if it doesn't, it represents a requirement to have "clean" clean ) and would likely end up leaving that portion of the business logic separate from whats in the application layer (I'm looking into how much of this can be housed solely in the ORM mapping files via uniquekey). But regardless of why EntityLoadByExample is being called, I still would expect it to to only return instances matching all defined properties - both data and object properties - still cannot nail down why it's not behaving this way. -Charlie On Wed, Dec 16, 2009 at 5:39 PM, Andrew Scott <[email protected]>wrote: > > Wouldn't throwing it in a try catch block be better than what your doing > here? > > And I would refactor this > > if(isNUll(EntityLoadByExample(x))) > x.save() > else > x = EntityLoadByExample(x) > > as > > x = EntityLoadByExample(x) > if(isNUll(x)) > x.save() > But I think for better understanding to what the code is doing placing it > in > a try catch block, and checking for the exception of a constraint error > would be better and easier to read and understand in 6-12 months time. > > On Thu, Dec 17, 2009 at 6:06 AM, Charlie Stell <[email protected] > >wrote: > > > > > I was using EntityLoadByExample today and the behavior wasn't what I was > > expecting. > > > > I expected it would be able to replace the query I always do right before > > insert (the "check" query) - to make sure that the insert wont conflict > > with > > unique constraints. > > > > Ive bee using it like this: > > > > x = entityNew("a") > > > > x.setP1("...") > > x.setP2("...") > > [...n] > > > > if(isNUll(EntityLoadByExample(x))) > > x.save() > > else > > x = EntityLoadByExample(x) > > > > This works great whenever all the properties for "a" are simple > > (fieldtype=column). > > > > But when I tried this with an object that has a many-to-one defined, it > > ignores this difference. > > > > I've dumped x and EntityLoadByExample(x) right next to each other and see > > that the "parent" object in the two dumps are different (different data, > to > > include pks). > > > > Right now Im using > > > > arrayLen(EntityLoad("a",{p1='#p1#',p2='#p2#',p3='#parent#'})) eq 0 in > place > > of isNUll(EntityLoadByExample(x)). > > > > This seems to do the trick. But does anyone know if I know > > if EntityLoadByExample should be able to handle this? > > > > > > > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| Want to reach the ColdFusion community with something they want? Let them know on the House of Fusion mailing lists Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:329201 Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4

