Loading Stats events from the database

Just like when loading SystemLogEvents from the database, you can use the provided StatsEvents class to retrieve and work with lists of StatsEvent objects.

For example, let's load the last fifty StatsEventHomeView objects from our earlier example, using what we learned in the Item lists section of the Items guide:

$statsEventItems = new \Cherrycake\Stats\StatsEvents([
    "p" => [
        "type" => "CherrycakeApp\StatsEventHomeView",
        "limit" => 50
    ]
]);

foreach ($statsEventItems as $statsEvent) {
    echo
        $e->Locale->formatTimestamp($statsEvent->timestamp).
        ": ".$statsEvent->typeDescription.
        ": ".$statsEvent->count.
				"\n";
}

As you can see, the StatsEvents class accepts some additional keys on top of the usual Items::fillFromParameters keys:

  • type Retrieves only StatsEvent objects of this class name. Must include the full namespace route to the class.

  • fromTimestamp Retrieves StatsEvent objects triggered after this timestamp.

  • toTimestamp Retrieves StatsEvent objects triggered up to this timestamp.

5/21/20: Home view: 90
5/22/20: Home view: 1

See examples of loading StatsEvent object from the database in the Triggering a stats event and Events with additional dimensions examples.

Last updated