CFWheels Nested Properties and hasManyCheckBox - coldfusion

I am trying to figure out how can I update the petevents table with several events. I get no error, but it is also not updating/inserting. Here are the relevant code snippets and the schema for the relevant tables follows.
View
<cfloop query="events">
#hasManyCheckBox(objectName="pet",
association="petevents",
keys="#pet.key()#,#events.id#",
label=events.eventname)#
</cfloop>
Pet Model
<cfset hasMany(name="petEvents", dependent="deleteAll", shortcut="events")>
<!--- nested properties --->
<cfset nestedProperties(associations="petEvents", allowDelete=true)>
Event Model
<cfset hasMany(name="petevents", dependent="deleteAll")>
PetEvent Model
<cfset belongsTo("pet")>
<cfset belongsTo(name="event", joinType="outer")>
View Update in Controller
<cfset pet = model("pet").findByKey(key=params.key)>
<cfset pet.update(params.pet)>
EDIT: I change the validatesPresenceOf property "when" to oncreate only. Then I saved and got this error. "Duplicate entry '1025-1025' for key 'PRIMARY'"
Thanks,
Derek

In your controller, don't forget to include petEvents:
<cfset pet = model("pet").findByKey(key=params.key, include="petEvents")>
EDIT:
In response to your dump below, can you tell me what you get when you do this in your update action?
<cfset pet = model("pet").findByKey(key=params.key, include="petEvents")>
<cfset pet.setProperties(params.pet)>
<cfdump var="#pet#" abort>
If that appears to be fine, what happens when you do this? Any errors?
<cfset pet = model("pet").findByKey(key=params.key, include="petEvents")>
<cfset pet.setProperties(params.pet)>
<cfset pet.update()>
<cfdump var="#pet.allErrors()#">
<cfloop array="#pet.petEvents#" index="petEvent">
<cfdump var="#petEvent.allErrors()#">
<cfloop>
<cfabort>

Related

How do I delete an item that is stored in the session?

I am using an array of structures in ColdFusion. Here is the code that I had tried. Can anyone help me to correct my code?
<cfif isDefined("remove")> //button in the cart page to remove a product
<cflock scope="session" type="readonly" timeout="0200">
<cfparam name="Session.cart">
<cfloop query="#qProductSelected#"> //this is the query for getting productid from url
<cfset sItem = structNew()> //this is my structure inside an array
<cfset sItem.Image= Application.imageUrl&qProductSelected.ProductImage> //for getting image
<cfset sItem.ProductId =#ProductId#> //getting productid
<cfset sItem.ProductImage = #Image#> //getting image
<cfset sItem.ProductName = #ProductName#> //getting product name
<cfset sItem.ProductDescription =#ProductDescription#> //getting productdescription
<cfset sItem.quantity = form.qty> //storing quantity from form into the session
<cfset structClear(sItem)> //finally i use structclear to clear the structure
</cfloop>
</cflock>
<cflocation url="cart.cfm"> //redirecting to cart page itself
</cfif>
Since you are using cart. I guess you are storing the products as array of structures with details of products stored in structure.
If you want to remove a product from cart the code will look something like this
<cfset ArrayDeleteAt(session.arrCart,form.productsequenceincart) />
So for example if someone clicked the remove button of 3rd product the above code would do this -
<cfset ArrayDeleteAt(session.arrCart,3) />
It would delete third product from the cart array.
We don't need to loop over the product.
If you are trying to remove the 'cart' key from session, you can do this:
<cfset StructDelete(session,'cart')>
However, you are in a readonly lock so you'd need to change it to be an exclusive lock or not using locking... depends on the situation as to what makes sense there.
StructDelete Docs

while updating nestedproperties record in cfwheels getting error

I have model which having many relationship with other table
<cfcomponent extends="Model">
<cffunction name="init">
<cfset table("products")>
<cfset hasOne(name="productInventory") />
<cfset hasmany(name="productDescriptions",dependent="deleteAll") />
<cfset hasmany(name="productPrices",dependent="deleteAll") />
<cfset nestedProperties(associations="productDescriptions,productPrices,productInventory",allowDelete=true)>
</cffunction>
</cfcomponent>
It's giving me an error on this update statement
Problem occured may because of structure created by cfwheels for object. as we look into to params.product dump having different id for many relation and in cfwheels object there is different id . may that why its creating problem of nested properties.
<cfset variables.product = model("product").findOne(include="productDescriptions,productPrices,productInventory", includeSoftDeletes="true", where="products.id=#params.product.id#") />
<cfset updateResult = product.update(params.product) />
Screen shot of error message:

ColdFusion Link to Previous Page Clears only that Page Session Variables

I have a page (form) set up like this:
<cfif not structKeyExists(session, "checkout")>
<cflocation url="ownerInfo.cfm" addToken="false">
</cfif>
<cfif not structKeyExists(session.checkout, "vehicle")>
<cfset session.checkout.vehicle = {ownership=""}
<cfparam name="form.ownership" default="#session.checkout.vehicle.ownership#">
<cfif structKeyExists(form, "submit")>
<cfset errors = []>
<cfif not arrayLen(errors)>
<cfset session.checkout.vehicle = {ownership=form.ownership}
<cflocation url="ownerCheck.cfm" addToken="false">
</cfif>
</cfif>
I am trying to figure out how I can reset this form by having a link on another page that when this page is linked back to it will reset all the session variables to null making the entire page needing to be filled out again.
This is what I have tried but am unsuccessful.
<cfif session.checkout.vehicle.ownership != null />
<cfset session.checkout.vehicle.ownership = null />
</cfif>
I cannot use <cfset StructClear(Session)> because I do not want all the session variables cleared from the previous pages only want this page to reset.(Not All Pages or All Session Variables). Any help with this would be greatly appreciated!
You can do using structDelete(structure,key)
<cfif session.checkout.vehicle.ownership != null />
<cfset structDelete(session.checkout.vehicle,'ownership ')>
</cfif>
If you want to clear the session scope variables if the ownership key already exists in the session scope then you can do:
<cfif structKeyExists(session.checkout.vehicle, "ownership")>
<!--- struct key exists so delete it --->
<cfset structDelete(session.checkout.vehicle, "ownership")>
</cfif>
You can actually just do:
<cfif structKeyExists(session.checkout, "vehicle")>
<!--- try and delete ownership key might not exist --->
<cfset structDelete(session.checkout.vehicle, "ownership")>
</cfif>
As long as the parent scope exists, you can try and delete the ownership key without first checking that it exists.
If you want to know if the key did exist then structDelete accepts a 3rd boolean parameter so it'll return true if it did exists and false if it didn't.
<cfset didExist = structDelete(session.checkout.vehicle, "ownership", true)>
An alternative approach to solving your problem would be to reset the form if it's not a form (POST) submission. So you'd do:
<cfif structKeyExists(form, "submit")>
<!--- form has been submitted store values in session and redirect... -->
<cfelse>
<!--- form not submitted so clear the session vars... --->
</cfif>

Getting data from the function using an Object in CF

I have a CFC object and a function which gets me the data which I want. Now I want to use that data and provide it to an already defined custom tag attribute. When I dump the #iEngine.listScore()# I get some parameters. But my problem is how should I provide those to an attribute?
<cfdump var="#iEngine.listScores()#" label="Swapnil Test - Function ListScore">
<cfset filename="ACE_DataExtract_#DateFormat(now(),'dd.mmm.yyyy')#.xls" />
<!--- Calling Custom tags to create/output xls files --->
<cfmodule template="#request.library.customtags.virtualpath#excel.cfm" file="#filename#" sheetname="ACE Report">
<cfmodule template="#request.library.customtags.virtualpath#exceldata.cfm"
query="#iEngine.listScores()#"
action="AddWorksheet"
sheetname="ACE Report"
colorscheme="blue"
useheaders="true"
contentformat="#{bold=true}#"
customheaders="#ListScore#">
<cfoutput>Excel Extract - ACE Report - #DateFormat(Now(),"d-mmm-yyyy")#</cfoutput>
</cfmodule>
</cfmodule>
Here I want to provide the data of iEngine.listScore() to the "Query" attribute in "exceldata" custom tag.
Below is the dump of iEngine.listScore()
I would write a transform Data function to change your array-struct to a query object, then pass that on....
<cffunction name="transformData" result="query">
<cfargument name="inArray" type="array">
<cfset local.qryReturn = queryNew("actiondate,actionId,closedate")>
<!--- You may look up queryNew and also set your dataTypes --->
<cfloop array="#arguments.inArray#" index="i">
<cfset QueryAddRow(local.qryReturn)>
<cfset querySetCell(local.qryReturn,"actionDate",i["actiondate"])>
<cfset querySetCell(local.qryReturn,"actionid",i["actionid"])>
<cfset querySetCell(local.qryReturn,"closedate",i["closedate"])>
</cfloop>
<cfreturn local.qryReturn>
</cffunction>
<cfset test = [
{actiondate='1/1/2015',actionid=134,closedate=''},
{actiondate='1/2/2015',actionid=135,closedate=''},
{actiondate='1/3/2015',actionid=136,closedate=''}
]>
<cfdump var="#test#">
<cfset resultQry = transformData(test)>
<cfif NOT isquery(resultQry)>
Exit invalid Data.
<cfelse>
<cfdump var="#resultQry#">
</cfif>

cfindex custom fields that are used for display no indexible

Currently working with Coldfusion 10 and cfsearch.
After many hours of trial and error, I have managed to get cfindex to collect all my custom field data so that it is available to me as fields for the output on my search results page.
My only issue now, is that the search will be able to search for things like true / false strings and it will return in the search results.
I could run a QoQ to strip those out, im just wondering if anyone has any suggestions for adding fields to an index but not having them searchable.
Any ideas greatly appreciated
my current code is below
<cftry>
<!-- create new searchable collection -->
<!--- cfcollection action= "create" collection="testsearch" path= "#expandPath('/assets/scripts/server/solr/')#" ---->
<cfcollection action= "list" name="collectionlist">
<cfdump var="#collectionlist#">
<cfquery name="MyQuery" datasource="#request.DSN#" maxrows="10">
SELECT *
FROM Store_products
</cfquery>
<cfdump var="#myQuery#">
<cfset myAttr = structNew()>
<cfset variables.columns = "COST,CREATEDDATE,CUSTOM_MESSAGE,CUSTOM_MESSAGE_LENGTH,DISPLAYNAME,DONTINCLUDEINSTOCKUPDATE,FEATUREDPRODUCT,FINALDAYS,FREEDELIVERY,HEIGHT,HTMLMETADESCRIPTION,HTMLMETAKEYWORDS,IMAGE1,IMAGE10,IMAGE2,IMAGE3,IMAGE4,IMAGE5,IMAGE6,IMAGE7,IMAGE8,IMAGE9,ISBUNDLE,ISNEWPRODUCT,ISONLINE,ISONSALE,ISSILENT,LONGDESCRIPTION,MANUFACTURER,NOVOUCHERS,ONLINEEXCLUSIVE,PARTNUMBER,PRICE,PRICELEVEL1,PRICELEVEL10,PRICELEVEL11,PRICELEVEL12,PRICELEVEL13,PRICELEVEL14,PRICELEVEL15,PRICELEVEL2,PRICELEVEL3,PRICELEVEL4,PRICELEVEL5,PRICELEVEL6,PRICELEVEL7,PRICELEVEL8,PRICELEVEL9,PRODUCTCATEGORY,PRODUCTCOLOR,PRODUCTMATERIAL,PRODUCTNAME,PRODUCTSIZE,PRODUCTSTYLE,PRODUCTTITLE,PRODUCTVIEWS,RRP,SALEEND,SALEPRICE,SALESTART,SHORTDESCRIPTION,SOLDOUT,STOCKQUANTITY,STORESORTORDER,TAXEXEMPT,VOLUME,WEIGHT,WIDTH">
<cfloop from="1" to="#ListLen(variables.columns,',')#" index="idx">
<cfset myAttr[listGetAt(variables.columns,idx,',') & "_s"] = listGetAt(variables.columns,idx,',') >
</cfloop>
<cfindex attributecollection="#myAttr#" action="update" collection="testsearch" type="custom" body="DISPLAYNAME,HTMLMETADESCRIPTION,HTMLMETAKEYWORDS,LONGDESCRIPTION,MANUFACTURER,PARTNUMBER,PRODUCTNAME,PRODUCTTITLE,RRP,SHORTDESCRIPTION" query="MyQuery" key= "productname">
<cfsearch name="searchResults" collection="testsearch" criteria="false" >
<cfdump var="#searchResults#">
<cfcatch type="any">
<cfdump var="#cfcatch#">
</cfcatch>
</cftry>