I'm creating a model entity in ColdBox.
component persistent="true" {
property name="id" type="int" fieldType="id" generator="increment";
property name="stamptime" type="timestamp";
property name="type" type="string" length="1" sqltype="varchar(1)";
property name="serial" type="string" length="100" sqltype="varchar(100)";}
the id field is set as identity and as primary key. the problem is i want to set serial field as unique key.. is there any way to set this field as unique key?
Have you tried defining in the property as follow:
component persistent="true" {
property name="serial" type="string" length="100" sqltype="varchar(100)" unique="true";
// and / or as a validation via constraints?
this.constraints = {
serial = { unique=true };
} //constraints
} //component
Related
I am dumping the following component.
Instead of doing Object.getUserID() manually each time, I want to get the primary key of the model dynamically based on property with fieldtype="id". Can anyone help on this?
https://cfdocs.org/cfproperty#p-fieldtype
This is how I declare my property:
<cfproperty name="UserID" fieldtype = "id" type="numeric"/>
getMetaData(Myobject).properties; gives me the array of structure.
I have my request body as:
<tns:InputRequest xmlns:tns="http://tempuri.org/">
<tns:ID>ID_001</tns:ID>
<tns:ID>ID_002</tns:ID>
<tns:Description>Description for ID_001</tns:Description>
<tns:Description>Description for ID_002</tns:Description>
</tns:InputRequest>
and to get the value of ID and Description, i Have created property as:
<property xmlns:tns="http://tempuri.org/" name="ID" expression="//tns:ID" scope="default" type="STRING"/>
<property xmlns:tns="http://tempuri.org/" name="Description" expression="//tns:Description" scope="default" type="STRING"/>
But this gets me only one value. How can i make a property array so that i can store multiple values of ID and description in it and how to retreive from this array property?Looking forward to your reply.Thanks in advance
You should be able to extract those values using XPATH (//node/child::node()) and then set to property.
Below thread will help you to extract required nodes and set to property. You need to set the type as 'OM' to preserve XML as it is.
how to catch an array of nodes to a property
In ColdFusion using ORM, how do I to map a foreign key column referring two tables?
My example.
At the moment an Image can belong to either a human or alien
Image.cfc
property name="imageID" fieldtype="id" generator="guid";
// Related Object Properties (many-to-one)
property name="Human" fieldtype="many-to-one" fkcolumn="HumanID" cfc="human";
property name="Alien" fieldtype="many-to-one" fkcolumn="AlienID" cfc="alien";
This leaves me with a table looking like this..
Image.cfc
ID NAME HumanID AlienID
1 img1 10 NULL
2 img2 NUll 8
To me NULLs smells bad!
So can I replace this with one mapping for both objects? Or If I create a category table? Any Ideas?
Thanks
Spark
You can't map a single property to more than one parent object. So you have two options here.
You will need to map this as a class per heirarchy where you will have classes for HumanImage and AlienImage that extend the base Image class
// Image.cfc
component persistent="true" {
property name="id" generator="guid";
property name="name";
}
// AlienImage.cfc & HumanImage.cfc
component persistent="true" extends="Image" joincolumn="id" {
}
// Alien.cfc
component persistent="true" {
property name="id" generator="guid";
property name="Image" fieldtype="one-to-many" cfc="AlienImage"
fkcolumn="fkAlienID";
}
// Human.cfc
component persistent="true" {
property name="id" generator="guid";
property name="Image" fieldtype="one-to-many" cfc="HumanImage"
fkcolumn="fkHumanID";
}
When attempting to do an EntitySave("publications",arguments); .. I receive the following error.
ids for this class must be manually assigned before calling save(): publications
I can't work out why.. My database primary keys are set up correctly, and I have setter=false these properties in my CFC. I have found a bit on this error doing a Google search, but nothing seems to indicate what is causing my issue here.
Here are my CFC's. Any pointers on what I might be doing wrong are appreciated. Thanks heaps in advance!
Publications.cfc
component persistent="true" table="publications"
hint="Publications"{
property name="id" fieldtype="id" setter="false";
property name="typeid" omrtype="int";
property name="name" ormtype="string";
property name="dateScheduled" ormtype="date" ;
property name="tstamp" ormtype="date";
property name="Article" fieldtype="one-to-many" cfc="publicationArticles" fkcolumn="publicationid";
}
publicationArticles.cfc
component persistent="true" table="publicationArticles"
hint="Publications"{
property name="id" fieldtype="id" setter="false" ;
property name="typeid" ormtype="int";
property name="title" ormtype="string" ;
property name="status" ormtype="boolean";
property name="publication" fieldtype="many-to-one" cfc="publications" fkcolumn="publicationid" ;
}
publicationTypes.cfc
component persistent="true" table="publicationTypes"
hint="Publicatin Type - Lookup"{
property name="id" fieldtype="id" setter="false" ;
property name="description" ormtype="string";
property name="publications" fieldtype="one-to-many" cfc="publications" fkcolumn="typeid" ;
}
You need a generator on the property.
property name="id" fieldtype="id" generator="identity";
Why does this not work in ColdFusion with ORM? I don't get any error but getval2() is blank
property name="ID" type="string" fieldtype="id" generator="guid";
property name="val1" type="string" ormtype="string" persistent=true;
property name="val2" type="any" persistent=false default="";
public statsEntity function init(){
variables.val2= this.getval1();
return Super.init();
}
what I think should happen is getval2() should be the value of val1.
Any ideas why this is not the case?
ORMExecuteQuery("from myTable")
Not sure I understand correctly, but init() only runs on new enitity so will NOT work here try postLoad() not init().