Skip to content

Model Object

key()

key() — returns string

Available in: model Category: Miscellaneous Functions

Returns the value of the primary key for the object. If you have a single primary key named id, then someObject.key() is functionally equivalent to someObject.id. This method is more useful when you do dynamic programming and don’t know the name of the primary key or when you use composite keys (in which case it’s convenient to use this method to get a list of both key values returned).

NameTypeRequiredDefaultDescription
$persistedbooleannofalse
$returnTickCountWhenNewbooleannofalse
// 1. Get the primary key value of a found object
employee = model("Employee").findByKey(params.key);
writeOutput(employee.key());
// -> 42

// 2. Use key() when you don't know the primary key column name (dynamic programming)
obj = model(params.modelName).findByKey(params.id);
if (IsObject(obj)) {
	writeOutput("Found record with key: " & obj.key());
}

// 3. Composite primary key — key() returns a comma-delimited list of both values
orderItem = model("OrderItem").findByKey(key="1,5");
writeOutput(orderItem.key());
// -> 1,5 (orderId and productId combined)