FROM
Dari provides from-related methods with specifiers that determine how a query operates in retrieving objects from the system.
from
method specifies a content type.fromType
specifies an object type.fromGroup
specifies a group as defined in object type definitions.fromAll
specifies all types.fromQuery
specifies an existing query.
from method
The simplest query is to select all records of a given content type, for example:
List<Activity> activities = Query.from(Activity.class).selectAll();
The from
method specifies the Activity
class as the target of the query, and the selectAll
method specifies that all instances of the Activity
class be retrieved. The from
method retrieves published objects.
When a base class is the target of the from
method, class inheritance impacts the results. In the following example, Activity
is the base class.
public class Activity extends Record { @Indexed private Date activityDate; @Indexed private User activityUser; @Indexed private String groupLead; } public class Checkin extends Activity { ... } public class Comment extends Activity { ... } public class ReadArticle extends Activity { ... } public class PostedArticle extends Activity { ... }
Given this class hierarchy, a query of the Activity
class implicitly retrieves any records that are subclasses of Activity
.
In addition to returning all results with the selectAll
method, the Query
API includes methods for limiting the number of returned results. One such method is first
, which returns only the first object of all of the instances retrieved.
User user = Query.from(User.class).first();
The Query
API includes several sort methods, two of which provide basic ascending or descending sort on a String
field of a class, for example:
List<User> users = Query.from(User.class).sortAscending("name").selectAll();
The field being sorted must have the @Recordable.Indexed annotation.
fromType method
The fromType
method specifies an object type definition of a class and returns instances of the specified definition. If the class derives from Content
, published instances are returned. For example, the following snippet gets the object type definition for Article
and specifies it in the fromType
method of the query. The query returns all published article instances.
ObjectType ot = ObjectType.getInstance(Article.class); return Query.fromType(ot).selectAll();
You can also specify an object type definition for a class that does not derive from Content
, returning instances that are not publishable. For example, the following snippet gets the object type definition for History
. The query returns versioned objects that have been converted to type History
.
ObjectType ot = ObjectType.getInstance(com.psddev.cms.db.History.class); return Query.fromType(ot).selectAll();
fromGroup method
The fromGroup
method queries objects of a specified group, as set in the group
attribute of the object type definitions. By default, Dari sets the group
attribute of all Record
-derived classes with a set of values that reflect class inheritance and implemented interfaces. Using the ObjectType
getGroups
and setGroups
methods, you can modify the values set on the group
attribute of object type definitions.
The fromGroup
method searches object type definitions with the specified group value. In the following example, activity.billable
is set on the group
attribute of the object type definition for Activity
. The query searches on that group value, returning all published Activity
objects, plus any other published objects whose definitions also include the activity.billable
group value.
ObjectType ot = ObjectType.getInstance(Activity.class); Set groups = ot.getGroups(); groups.add("activity.billable"); ot.setGroups(groups); ot.save(); List<Object> objects = Query.fromGroup("activity.billable").selectAll();
fromAll method
The fromAll
method specifies all types, for example:
return Query.fromAll().selectAll();
The fromAll
method returns published and unpublished objects, including objects hidden from the UI as described in visibility-indexed fields.
fromQuery method
The fromQuery
method specifies an existing query. The method is useful for applying additional predicates to a user-created query that you want to filter. For example, the following query specifies an existing query (endUserQuery
) and uses the where
method to filter for a specific object type.
List<Article> articles = Query.fromQuery(endUserQuery).where("_type = ?", "00000162-3fac-db0a-a177-ffef80210000").selectAll();