Return to v2.0 docs

findByKey()


Model Class Read Functions any model


Fetches the requested record by primary key and returns it as an object. Returns false if no record is found. You can override this behavior to return a cfquery result set instead, similar to what's described in the documentation for findOne().

Name Type Required Default Description
key any Yes Primary key value(s) of the record. Separate with comma if passing in multiple primary key values. Accepts a string, list, or a numeric value.
select string No Determines how the SELECT clause for the query used to return data will look. You can pass in a list of the properties (which map to columns) that you want returned from your table(s). If you don't set this argument at all, CFWheels will select all properties from your table(s). If you specify a table name (e.g. users.email) or alias a column (e.g. fn AS firstName) in the list, then the entire list will be passed through unchanged and used in the SELECT clause of the query. By default, all column names in tables joined via the include argument will be prepended with the singular version of the included table name.
include string No Associations that should be included in the query using INNER or LEFT OUTER joins (which join type that is used depends on how the association has been set up in your model). If all included associations are set on the current model, you can specify them in a list (e.g. department,addresses,emails). You can build more complex include strings by using parentheses when the association is set on an included model, like album(artist(genre)), for example. These complex include strings only work when returnAs is set to query though.
handle string No query Handle to use for the query. This is used to set the name of the query in the debug output (which otherwise defaults to userFindOneQuery for example).
cache any No If you want to cache the query, you can do so by specifying the number of minutes you want to cache the query for here. If you set it to true, the default cache time will be used (60 minutes).
reload boolean No false Set to true to force CFWheels to query the database even though an identical query for this model may have been run in the same request. (The default in CFWheels is to get the second query from the model's request-level cache.)
parameterize any No true Set to true to use cfqueryparam on all columns, or pass in a list of property names to use cfqueryparam on those only.
returnAs string No object Set to objects to return an array of objects, set to structs to return an array of structs, or set to query to return a query result set.
callbacks boolean No true Set to false to disable callbacks for this method.
includeSoftDeletes boolean No false Set to true to include soft-deleted records in the queries that this method runs.
// Getting the author with the primary key value `99` as an object
auth = model("author").findByKey(99);
// Getting an author based on a form/URL value and then checking if it was found
auth = model("author").findByKey(params.key);
if(!isObject(auth)){
flashInsert(message="Author #params.key# was not found");
redirectTo(back=true);
}
// If you have a `belongsTo` association setup from `comment` to `post`, you can do a scoped call. (The `post` method below will call `model("post").findByKey(comment.postId)` internally)
comment = model("comment").findByKey(params.commentId);
post = comment.post();

Related Functions

Read Functions