Get Counter Values
-
Use
countersFor.get
to retrieve the values of specific counters,
orcountersFor.getAll
to retrieve the names and values of all counters associated with a document. -
For all other
countersFor
methods see this Overview. -
In this page:
Get values of specific counters
get
usage:
- Open a session
-
Create an instance of
countersFor
.- Either pass
countersFor
an explicit document ID, -or- - Pass it an entity tracked by the session, e.g. a document object returned from session.query or from session.load.
- Either pass
-
Call
countersFor.get
to retrieve counter value/s.get("CounterName")
will return a singleint
value for the specified counter.get(["counter1", "counter2"]
will return an array with values for all listed counters.
E.g.,[ "counter1" => 1, "counter2" => 5 ]
get
example:
// 1. Open a session
$session = $docStore->openSession();
try {
// 2. pass the countersFor constructor a document ID
$documentCounters = $session->countersFor("products/1-C");
// 3. Use `countersFor.get` to retrieve a counter's value
$daysLeft = $documentCounters->get("DaysLeftForSale");
echo "Days Left For Sale: " . $daysLeft . PHP_EOL;
} finally {
$session->close();
}
get
syntax:
public function get(string|StringList|array $counters): null|int|array;
Parameter | Type | Description |
---|---|---|
counters |
string or StringList or array |
Counter names |
Return Type | Description |
---|---|
int |
Counter's current value |
Get all Counters of a document
getAll
usage:
- Open a session.
-
Create an instance of
countersFor
.- Either pass
countersFor
an explicit document ID, -or- - Pass it an entity tracked by the session, e.g. a document object returned from session.query or from session.load.
- Either pass
- Call
countersFor.getAll
to retrieve the names and values of all counters associated with the document.
getAll
example:
// 1. Open a session
$session = $docStore->openSession();
try {
// 2. Pass the countersFor constructor a document ID
$documentCounters = $session->countersFor("products/1-C");
// 3. Use getAll to retrieve all of the document's Counters' names and values
$counters = $documentCounters->getAll();
// list counters' names and values
foreach ($counters as $counterKey => $counterValue)
{
echo "counter name: " . $counterKey . ", counter value: " . $counterValue;
}
} finally {
$session->close();
}
getAll
syntax:
public function getAll(): array;
Return Type | Description |
---|---|
array |
An array of Counter names and values |