How to Leverage the Persistable.evaluate() method


c3ShowFunc(Persistable, "evaluate") to learn more about the method.

The evaluate method allows standard SQL-like groupings and aggregations to be done through the type system API’s. As you’ve noticed, one record per entry in FetchedType is returned when performing a fetch() call. evaluate allows for a more user-configured aggregation of the response with the aggregations happening during the execution of the query at the database level

Pay particular attention the EvaluateSpec input parameter type. You will notice that many of the fields are similar to that of the FetchSpec, but there are a few important, new fields to investigate: group and projection.

Here are some example calls and responses on the Unit type:

c3Table(Unit.evaluate({projection: "concept, count()", group: "concept", filter: "startsWith(concept, 'm')"}))

yields a count of the unit records by concept which meet the filter criteria:

Note that including the id in the projection field, essentially turns this back into a fetch, where one record per row in the type is returned. Also note that I can perform logic on the fields returned with any c3ShowType(ExpressionEngineFunction). Here’s a simple, likely functionally useless, but insightful example:

c3Table(Unit.evaluate({projection: "substring(concept, 0, 3), count()", group: "substring(concept, 0, 3)", filter: "startsWith(concept, 'm')"}))

and its results:

1 Like